404错误快速路由

时间:2018-05-14 09:41:50

标签: node.js express routing

我无法解决这个简单路由任务出错的地方。当我去localhost:3030 /工作人员时,我得到了"无法获得/工作人员"和404错误。

这是我的设置。

app.js:

const express = require('express');
const app = express();
const port = process.env.PORT || 3030;

const staffRouter = require('./routes/staffrouter.js');

app.use('/static', express.static(__dirname + './public'));

app.use('./staff', staffRouter);

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

app.listen(port);

我尝试过使用路径" / staff"在我的GET / POST请求中,但这不起作用,并且根据我正在做的教程,它不应该如何工作。我真的卡住了。

/routes/staffrouter.js:

var express = require('express');
var router = express.Router();

const staff = require('../staff').staff;
const urlEncoded = (express.urlencoded({ extended: true }));

router.get('/', function(req, res, next) {
    res.render('staff.pug', { 
        deptOptions: staff.populateSelectors('department'),
        posOptions: staff.populateSelectors('position'),
        empArray: staff.readWriteJSON()
    });
 });

// Add new staff obj
router.post('/', urlEncoded, function(req, res, next) {
    let sObj = req.body;
    let dataArray = staff.readWriteJSON();

    //console.log('data:', data);
    dataArray.push(new staff.Employee(
        sObj.fName, 
        sObj.lName, 
        sObj.staffNum, 
        sObj.department, 
        sObj.position, 
        sObj.email, 
        sObj.phone
     ));
    staff.readWriteJSON(dataArray)
    res.render('../views/staff.pug', { 
        deptOptions: staff.populateSelectors('department'),
        posOptions: staff.populateSelectors('position'),
        empArray: staff.readWriteJSON()
    });
});

module.exports = router;

1 个答案:

答案 0 :(得分:1)

错误的范围改变了订单

app.use('/static', express.static(__dirname + './public'));
const staffRouter = require('./routes/staffrouter.js');