node.js - Express build project 404
大家讲道理
大家讲道理 2017-05-24 11:38:45
0
2
783

1. Use express4 to create a project, and its default "/" and "/users" paths can be accessed, so add the path and path files based on its entry file, and create the corresponding ejs template. The result is the homepage Able to access and connect to the style file normally, other paths display 404.



PS: Sorry for not explaining clearly how the error occurred: when running debugging locally, localhost:3000 can access the homepage, but localhost:3000/login cannot point to the login page and 404 appears. The document structure is as follows:
test
|
---public
|
---routes
    |
    ---login.js(路由文件)
|
---view
    |
    ---login.ejs(模板文件)
|
---app.js(入口)
//app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var login = require('./routes/login');
var register = require('./routes/register');
var logout = require('./routes/logout');
var post = require('./routes/post');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/login', login);
app.use('/register', register);
app.use('/logout', logout);
app.use('/post', post);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
//路径文件之一(login.js)
var express = require('express');
var router = express.Router();

/* GET login listing and render page. */
router.get('/login', function(req, res, next) {
  res.render('login', { title: '登录' });
});

module.exports = router;
//模板文件之一(这块应该没问题,被引入的我就不贴了)(index.ejs)
<%- include header %>
主页
<%- include footer %>

Please give me your answer.

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
为情所困

//One of the path files (login.js)
router.get('/', function(req, res, next) {
res.render('login', { title: 'Login' });
} );

Just change it to this

Because the matching path of
app.use('/login',router);
router.get('/login',()=>{})
is /login/login

小葫芦

Change the access path to localhost:3000/login/login

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template