简体   繁体   中英

404 Cannot POST Using Express

I´m trying to create an API Rest in NodeJs with Express.

I have my index.js with the following code:

const express = require('express');
const routesV1 = require('./routes/v1/index');

const app = express();

routesV1(app);

app.listen(4000, () => {
  console.log('Running on port: 4000');
});

then i have a routes folder with another "index.js" and a "usersRoutes".

Here´s the code of the userRoutes:

const express = require('express');

const userController = require('../../controllers/v1/UserController');

const router = express.Router();

router.post('/create', userController.createUser);
router.post('/update', userController.updateUser);
router.post('/delete', userController.deleteUser);
router.get('/get-all', userController.getUsers);

module.exports = router;

and the code of the index.js into the routes folder:

const userRoutes = require('./UserRoutes');
const productRoutes = require('./ProductRoutes');

module.exports = app => {
  app.use('api/v1/users', userRoutes);
};

but when i´m consuming via Postman, with the following url: http://localhost:4000/api/v1/users/create I´m receiving a 404 error code.

Can anybody help me? thanks

I resolved it adding '/' to the route in routes/index.js

const userRoutes = require('./UserRoutes');
const productRoutes = require('./ProductRoutes');

module.exports = app => {
  app.use('/api/v1/users', userRoutes);
  app.use('/api/v1/products', productRoutes);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM