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

前端 未结 4 507
故里飘歌
故里飘歌 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:03

    in order to access /test use router.get('/' and app.use('/test' because express will concatenate / and /test.

    in your case you have to access /test/test so do this and enter /test in your borwser:

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

    Then, in your server.js :

    const test = require('./server/routes/test'); app.use('/test', test);
    

提交回复
热议问题