I am building a simple nodejs server to serve static content for a webpage. I want to send status codes back but when I try to create a function for the app.use() the content is not served.
var express = require('express')
var app = express();
app.listen(3000, function() {
console.log("We are now listening on Port:3000")
app.use(express.static('public'));
app.use('*', express.static('public/404.html'));
});
The content is served when I run this, but I cannot get the status codes as I need. Any help is appreciated.
I don't know exactly what you want. This is my suggest solution:
var express = require('express')
var app = express();
app.listen(3000, function() {
console.log("We are now listening on Port:3000")
app.use(express.static('public'));
app.use('*', function(req, res) {
// change your status code here
res.status(404).sendFile(__dirname + "/public/404.html");
});
});
Related
I'm learning Js.
I have a problem in the code:
const express = require('express');
const app = express();
const PORT = 3000;
app.listen(PORT, function(){
console.log("The Express is running");
});
app.get('/', (req,res)=>{
res.send('Ok');
});
running node app.js i have the answer "Ok".But, running in the browser(localhost:3000) i get : "The connection was refused".
necessary info: I'm use a container docker, which I use the ubuntu. Can this cause a problem?
I'm trying to create 2 seperate files in my app,
one for API request, and second one for socket.io
The starting file is app.js which look like this:
const express = require('express');
const app = express();
app.get('/test', (req, res) => {
res.send('test');
});
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
module.exports = app;
and the second file for socket.io looks like this:
const app = require('./app');
const http = require('http').Server(app);
const io = require('socket.io')(http);
...
http.listen(port, function(){
console.log('listening on *: ',port);
});
When I run the app with my IDE (VSCODE) everything works perfect,
but when I'm trying to run on the terminal the app doesn't start and the page isn't available on the starting port.
I think that the problem is about the export for the socketIO file in app.js,
Any help would be appreciated.
I am currently working on a project which is a webpage using angular to dynamically change DOM elements. Within the project is a public folder which contains all HTML, CSS, JavaScript and JSON objects. The project must be distributed so I am using node to run from localhost. This is my server.js code:
var express = require('express');
var app = express();
var path = require('path');
app.get('/', function(req,res){
res.sendFile(path.join(__dirname + '/public'));
});
app.listen(8080, function(){
console.log((new Date()) + " Server is listening on port 8080");
});
When I head to localhost:8080 it just says Cannot GET /. Where am I going wrong?
The correct way to serve static files with express is as follows:
//Look for statics first
app.use(express.static(path.join(__dirname, 'public')));
//Return the index for any other GET request
app.get('/*', function (req, res) {
res.sendFile('index.html', {root: path.join(__dirname, 'public')});
});
Edit: on a side note this may be worthwhile to mention that app.get should be the last route declared in node so if you want API endpoints exposed declare them above (before) the final app.get.
You forgot to point to the actual html file you want to display. If you have a index.html in your public directory, just point tot '/public/index.html' . That works (tested it here).
Followed user Muli's answer and all files are now being served correctly. Code here:
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req,res){
res.sendFile('index.html', {root: path.join(__dirname, './public')})
});
app.listen(8080, function(){
console.log((new Date()) + " Server is listening on port 8080");
});
I referred to the previously asked question on this but couldnt resolve it. I have Express server installed and trying to run Index.html file through it.
But I am getting 'Cannot GET /' as the response.
Here is the server.js through which I calling the index.html
var express = require('express');
var app = express();
app.get('index.html', function (req, res) {
app.use("/", express.static(__dirname));
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
Thanks in advance!!
When you access a directory on your hosted site, say the root directory of localhost on port 8080, using http://localhost:8080/ for URL, the browser does not send a request for 'index.html` to the server and just uses whatever the server sends back.
It's the express static middleware which in response to a browser request with no filename will check if the folder exists and (by default) return any index.html contained in the folder. So your line of code for routing, app.get('index.html') never executes, and the browser gives you the error message.
Here's a mini static express server if you want to try it.
var express = require('express');
var app = express();
app.use(express.static('../public')); // path to your public directory
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
If you want a simple static, folder-as-server kind of thing, you can do it without express way like "/public":
var fs = require("fs");
var host = "localhost";
var port = 8000;
var express = require("express");
var app = express();
app.use('/', express.static(__dirname));
app.listen(port, host);
I put this in the file express.js, so that is in the same folder than index.html (even associated .js with node.exe). This way the folder is the root of the server.
I can set up the chat no problem, but my question is how do I get the chat to work on the default port:80 where my main site is?
First thing that comes to mind is iframing it?
Here is my server.js code, one thing to note I don't really like jade so I am going to convert that into plain HTML. Also for the chat to work it has to be on any port other than :80 so I am not quite sure how to get it to work on that main port, other than iframing?
So my question is obviously what are my options in getting the express server to work on the main port?
// Start server
var express = require("express");
var app = express();
var port = 3700;
// Directory
app.use(express.static(__dirname + '/chat'));
// Socket.io integration
var io = require('socket.io').listen(app.listen(port));
// Render content
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
app.get("/", function(req, res){
res.render("page");
});
// Recieve msg and send
io.sockets.on('connection', function (socket) {
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
You dont realy need node-expres-jade on the port 80 for these aproach.
Node can just run the socket server (alone). Forget express/jade here.
And apache or nginx can handle all de client-side files (html, js , css ...).
add the var socket = io.connect('httt://yoururl:port'); on any html an it should work.