无法GET / users / test nodejs

时间:2018-02-06 01:57:56

标签: javascript node.js express

我正在尝试为特定用户个人资料页面加载/ users /(用户名),但是我收到Cannot GET /users/test错误。我让它在某一点工作,但后来我添加了一个功能,它再次被打破(我认为问题可能在路线中)。这是相关文件。任何帮助将不胜感激。谢谢!

app.js:

const fs = require('fs');
const _ = require('lodash');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongo = require('mongodb');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/tipcup');
var db = mongoose.connection;

const routes = require('./routes/index');
const users = require('./routes/users');
const user = require('./routes/user');

// Init App
var app = express();

// View Engine
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'layout'}));
app.set('view engine', 'handlebars');

// BodyParser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.use(cookieParser());

// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));

// Express Session
app.use(session({
  secret: 'secret',
  saveUninitialized: true,
  resave: true
}));

// Passport Init
app.use(passport.initialize());
app.use(passport.session());

// Express Validator
app.use(expressValidator ({
  errorFormatter: function(param, msg, value) {
    var namespace = param.split('.')
    , root = namespace.shift()
    , formParam = root;

    while(namespace.length){
      formParam += '[' + namespace.shift() + ']';
    }
    return {
      param: formParam,
      msg: msg,
      value: value
    };
  }
}));

// Connect Flash
app.use(flash());

// Global Vars
app.use(function (req, res, next){
  res.locals.success_msg = req.flash('success_msg');
  res.locals.error_msg = req.flash('error_msg');
  res.locals.error = req.flash('error');
  res.locals.user = req.user || null;
  next();
});

app.use('/', routes);
app.use('/users', users);
app.use('/user/:username', user);

// Set Port
app.set('port', (process.env.PORT || 3000));

app.listen(app.get('port'), function(){
  console.log('Server started on port '+app.get('port'));
});

user.js route:

var express = require('express');
var router = express.Router();
var User = require('../models/user');

// GET user by username
router.get('/:username', function(req, res) {
  //var username = req.params.username;
  User.getUserByUsername(function(err, user) {

    if(err) {
        res.send('error');
        next();
    }

    const vm = user;
    res.render('user', vm);

  });
  //res.render('user');
});

module.exports = router;

2 个答案:

答案 0 :(得分:2)

看起来您要定义用户ID两次

app.js中,您正在使用行

挂载用户路由/user/:username

app.use('/user/:username', user);

然后在user.js中,您宣布使用该行获取/:username

router.get('/:username', function(req, res) {

我认为这会产生一个带有签名/user/:username/:username

的路线

我建议删除/:username其中一个文件

答案 1 :(得分:1)

router.get('/:username', function(req, res) {
      var username = req.params.username;

      User.getUserByUsername(username).then(function(err, user){
        res.json(user);
      });

对于猫鼬,你可以这样做:

router.get('/:username', function(req, res) {
      var username = req.params.username;

      User.getUserByUsername(username,function(err, user){
        res.json(user);
      });
});