0

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;
}

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

in the mentioned boiler plate library, can u explain the code /** POST / - Create a new entity */ create({ body }, res) { body.id = facets.length.toString(36); facets.push(body); res.json(body); }, in the facets.js please , and if i want to use it in the router how do i do that.
Yes. That is the callback function for the POST /facets route. It takes 2 params: (req, res) = (request, response). The author used { body } instead of req. That’s deconstructing. He did that because he only needed the body param from the request body. This callback function adds the new created facet to the facets array, and returns the newly created facet. Based on my answer you can write like this: router.post(‘/create’, (req, res) => { let body = { req } body.id = facets.length.toString(36); facets.push(body); res.json(body); }
i understood your answer and completely working fine, thanks. but if i want to use create route as per the author code, how do i do that. can u please explain.
I don't think you can(based on what I've ready about that library), and honestly I don't find much benefit in using it. I'm a node.js developer and I personally don't remember seeing any types of libraries like this used in production. If you really want to create routes using "resource-router-middleware" you'll have to settle with default route names.
Sorry, I meant the default "/" route name. Meaning if your express router is using a router created with "resource-router-middleware" like in: api.use('/facets', facets({ config, db })) your route names will be based solely on what you use on the main router("/facets") and the router you import will have "/" default route name for all the routes, and will be differentiated only by http method (get, post, put, delete).
0

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');
});

1 Comment

i need to understand the project, in the facets.js file, how do i extend for different route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.