Node Js Express Routes
Express js, we can create the many API by using the node express js,
API Routes
API Method like GET, POST, PUT, and DELETE
First we need to create the controller file for app using express, and save with your controller name.
Create the "User.js"
var express = require('express'); var router = express.Router(); //Can craete the api like get the user list router.get('/list', function(req, res){ res.send('GET route.'); }) //can use this api like register user router.post('/save', function(req, res){ res.send('POST route.'); }); //can use this api like delete user router.delete('/:id', function(req, res){ res.send('DELETE route.'); }); //can use this api like register/update user router.put('/update', function(req, res){ res.send('PUT route.'); }); module.exports = router;
Then after we need to create the node application using express
Create the index.js
const express = require('express'); const app = express(); //import the controller file and use var UserController = require('./User'); app.use('/user', UserController); const PORT = process.env.PORT || 8080; app.listen(PORT, ()=>{ console.log(`Server Started on port ${PORT}`); });
Run server using following command
node index.js
You can access the API on http://localhost:8080
API Calls
GET - http://localhost:8080/user/list
POST - http://localhost:8080/user/save
DELETE - http://localhost:8080/user/{id}
PUT - http://localhost:8080/user/update/{id}
Comments (0)
Post a Comment
Cancel