无法在 Express NodeJS 中配置路由器

Cannot configure Router in Express NodeJS

我有下一个服务器文件:

'use strict'

const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server);

const index = require('./routes/index');
const chat = require('./routes/chat');

app.use('/', index);
app.use('/chat', chat);

const port = process.env.API_PORT || 8989;
server.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

以及 ./routes 目录中的下两条路线 index.jschat.js

// ./routes/index.js

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

router.route('/')
    .get((req, res) => {
        res.json('Hello on the Homepage!');
    });

module.exports = router;



// ./routes/chat.js

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

router.route('/chat')
    .get((req, res) => {
        res.json('Hello on the Chatpage!');
    });

module.exports = router;

第一个 index.js 通过标准端口 localhost:8989/ 正常加载,但是当我通过 localhost:8989/chat 获得第二条路由时 - 我总是收到 error - Cannot GET /chat...

我做错了什么?

server.js

const index = require('./routes/index');
const chat = require('./routes/chat');


app.use('/chat', chat); // when path is :/chat/bla/foo
app.use('/', index); 

./routes/index.js

router.route('/')
    .get((req, res) => {
        res.json('Hello on the Homepage!');
    });

./routes/chat.js

// It is already in `:/chat`. There we need to map rest part of URL.
router.route('/')  
    .get((req, res) => {
        res.json('Hello on the Chatpage!');
    });
// ./routes/chat.js

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

router.route('/')
    .get((req, res) => {
        res.json('Hello on the Chatpage!');
    });

module.exports = router;

你可以用这个