如何调试Node.js Web应用程序中未找到的错误?

时间:2018-10-26 05:37:07

标签: node.js mongodb express

我正在研究基于Express Framework的Nodejs Web应用程序项目。我正在创建一个注册页面。输入用户名和密码后,网址应重定向到个人资料页面。在views文件夹中,我创建了一个文件“ profile.hbs”。但是个人资料无法识别,并且找不到404。请分享您有关如何调试此问题的想法。谢谢。

  

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var expressHbs=require('express-handlebars');
var mongoose=require('mongoose');
var session=require('express-session');
var passport=require('passport');
var flash=require('connect-flash');
const mocha=require('mocha');
var validator=require('express-validator');

var indexRouter = require('./routes/index');
//var usersRouter = require('./routes/users');

var app = express();
mongoose.connect('mongodb://localhost:27017/shopping');
require('./config/passport');

// view engine setup
app.engine('.hbs',expressHbs({defaultLayout:'layout',extname:'.hbs'}));
app.set('view engine', '.hbs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(validator());
app.use(cookieParser());
app.use(session({secret:'mysupersecret',resave:false,saveUninitialized:false}));
app.use(passport.initialize());
app.use(flash());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
//app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// 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;
  

路由文件

var express = require('express');
var router = express.Router();
var csrf=require('csurf');
var passport=require('passport');
const canine= require('../models/dogfood');
var csrfProtection=csrf();
router.use(csrfProtection);

/* GET home page. */
router.get('/', function(req, res, next) {
    canine.find(function(err,docs){
        var productChunks=[];
        var chunkSize=3;
        for(var i=0;i<docs.length;i += chunkSize){
            productChunks.push(docs.slice(i,i+chunkSize));
        }
        res.render('shop/index', { title: 'Express',diets:productChunks});
    });
});
router.get('/user/signup',function(req,res,next){
    res.render('user/signup',{csrfToken:req.csrfToken()})
});
router.post('user/signup',passport.authenticate('local.signup',{
    successRedirect:'user/profile',
    failureRedirect:'user/signup',
    failureFlash:true
}));
router.get('user/profile',function(req,res,next) {
        res.render('user/profile');
});
module.exports = router;

1 个答案:

答案 0 :(得分:2)

您在路线名称中有错字。应该是'/user/profile'

router.get('/user/profile', function(req,res,next) ...