简体   繁体   中英

use express nodejs rest api boilerplate to create new route

I am new to the expressjs, I am planning to use this boilerplate for my rest api project. I want to know how do i extend the existing routes to update or create rest api. out of the box if i run the code it works for http://localhost:8080/api/facets/ I want to extend the route like http://localhost:8080/api/facets/create or http://localhost:8080/api/facets/list

i am confused in the file express-es6-rest-api/src/api/index.js and express-es6-rest-api/src/api/facets.js

please explain below code:

export default ({ config, db }) => {
    let api = Router();
    // mount the facets resource
    api.use('/facets', facets({ config}));
    // perhaps expose some API metadata at the root
    api.get('/', (req, res) => {
        console.log(api)
        res.json({ version });
    });


 return api;
}

I suggest for you to create another facets file inside src/api and try to create your own express router without any libraries like "resource-router-middleware".

Please read https://expressjs.com/en/guide/routing.html .

Here you can see how easily you can create another express router that you can import inside index.js and use instead of the existing one based on "resource-router-middleware". This way you'll have full control of the routes including whatever names you want.

I'll give a quick example of what I mean with the easiest route in the repo:

import { Router } from 'express'

let router = Router()

router.get('/get', (req, res) => {
    res.json(facets)
})

export default router

If you import this router inside "index.js" and use it inside: "api.use('/facets, newFacetsRouter)" where newFacetsRouter is the imported router from above, you'll see that you can now call GET "/facets/get" instead of the previous GET "/facets" . You can continue the code I posted for all the methods with the following pattern:

router.["HTTP METHOD"]('/["ROUTE NAME"]', callback)

Where "HTTP METHOD" can be: "get, post, put, delete". "ROUTE NAME" is whatever you want it to be. And the callback is the function that executes after the http call is successful.

And regarding the code you posted you wanted to have it explained: it's just an express router, where we import another express router inside, again please read express docs about routing.

Its a bit hard to grasp what you really need help with. Do you need help understanding the project and the code or do you wish to learn how to create new endpoints for your API? So let me show you a simple little CRUD api for facets written out.

Install express and a body parser

npm i -S express
npm i -S body-parser

Setup the server, add the body parser and register the endpoints you wish to use.

const express = require('express');
const json = require('body-parser');

const facets = [];

const app = express();
app.use(json());

app.get('/api/facets', (req, res) => {
    res.send(facets);
});

app.post('/api/facets', (req, res) => {
    facets.push(req.body);
    res.send(req.body);
});

app.put('/api/facets/:index', (req, res) => {
    facets[req.param.index] = req.body;
    res.send(req.body);
});

app.delete('/api/facets/:index', ((req, res) => {
    facets.splice(req.param.index, 1);
    res.send(facets);
}));

app.listen(1337, () => {
    console.log('Server is runing');
});

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