nodejs Route.get() requires callback functions but got a [object String]

前端 未结 4 454
故里飘歌
故里飘歌 2021-01-27 05:24

I\'m starting coding using nodejs with express. So i did this in my file test.js which is into my folder routes :

const express = require(\'express\');

const ro         


        
4条回答
  •  囚心锁ツ
    2021-01-27 06:09

    The problems seems to be how you are mounting the router. Looking the the router middleware API it seems you should be doing it like this.

    test.js

    const express = require('express');
    const router = new express.Router();
    
    router.get('/test', (req, res, next) => {
      res.send("I'm a test");
      next();
    });
    
    module.exports = router;
    

    server.js

    const express = require('express');
    const app = express();
    const test = require('./test'); 
    
    app.use('/', test);
    
    app.listen(3000);
    

提交回复
热议问题