Post method in each route of express

at the beginning of learning node+express, there is a question that is not very clear. I would like to ask you a question:

after a new express project is created by default, there are two pages: index and user
I need to write a post interface in the user page:

clipboard.png
ajax404 Not Found

clipboard.png

clipboard.png
, but if I put the post API test in user.js into the ingress page index.js, and then call it in user.jade, it will work normally.
I would like to ask:
whether all APIs need to be placed on the ingress page before they can be called; what should I do if I want to put them in different routes?

= Thank you for your great solution =

Mar.17,2021

how do you router use ?
app.use ('/ name',router)
then your route is / name/test


router = export.Router () is a convenient (universal prefix) operation, as well as a convenient modular operation. You need to hang router under app.

app.use ('/', router) , if the slash is replaced with another prefix, as described on the first floor


//routert.js
router.get('/', function(req, res) {
  
});
router.post('/test', function(req, res) {
});

//nodejs run js,app.js

//
var app = express();
var router = require('./router');
...
app.use('/user', router );
// /user /user/test

ide/routing.html" rel=" nofollow noreferrer "> http://www.expressjs.com.cn/g.

Menu