简体   繁体   中英

Express JS with Node.JS

I am having a problem with my website. I am running Ubuntu 16.04.1 x64 with node.js. For my website, whenever I go to the other pages on my website. It directs them to the 404 page even though I have a routing set for it.

Here is my server.js file:

var http = require('http');
var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');

var app = express();

app.use('/',express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({ extended: false }));
app.engine('.html', require('ejs').__express);
app.set('view engine','html');


app.get('/', function(req, res){
    res.render('index');
});

app.get('/resume', function(req, res){
    res.render('resume');
});

app.get('/contact', function(req, res){
    res.render('contact');
});

app.use(function(req, res) {
    res.status(404).render('404');
});

app.listen(3000, function () {
    console.log('Listening on port 3000!');
});

For my other configurations for nginx, everything has been based from this guide here: https://code.lengstorf.com/deploy-nodejs-ssl-digitalocean/

Based on your express static config and your route configurations express is expecting index.html , resume.html and contact.html in the views directory.

By default the express res.render() method expects your views in the ./views folder: http://expressjs.com/en/4x/api.html#app.render

So your project should look like the following:

www
|-- views
|    |-- index.html 
|    |-- resume.html 
|    +-- contact.html
|-- node_modules
|    + ...
|-- index.js
+-- package.json

some tips, you are not using your import http, so you can remove it, then you are using a custom middleware which is normally using 3 params req, res, next . And you can remove the dot in front of html => app.engine('.html', require('ejs').__express);

Then for your problem, it should be a configuration problem, not coming from nodeJS, I tried on my machine, and everything is ok with the code you sent (assuming you have your views in a directory called views, with the name index.html etc.)

So you should look at your nginx/apache configuration

Found the solution for this. I have tried running this command: DEBUG=express:* node server.js and it returned with an error containing this

Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at Server._listen2 (net.js:1257:14)
at listen (net.js:1293:10)
at Server.listen (net.js:1389:5)
at EventEmitter.listen (/apps/michaeluy.me/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/apps/michaeluy.me/server.js:30:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)

Now I simply changed 3000 to 5000 on my server.js file as seen here:

app.listen(5000, function () {
console.log('Listening on port 5000!');
});

and now. It works perfectly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM