DEV Community

Discussion on: Node backend and api calls same project

Collapse
 
kryz profile image
Kryz

Hello D0xzen,
you don't need to create a new project.

Add a new api route to your express instance to handle all requests that come from your frontend javascript code, for example:

app.post("/api/posts", (req, res) => {
   // call your service/model or directly db instance
   addPostToDB(req.body)
     .then(newPost => res.json(newPost)
     .catch(err => res.status(500).json({msg: "Cannot add a new post"});
};

or for likes:

app.post("/api/likes/:postId", (req, res) => {
   // call your service/model or directly db instance
   updateLikes(req.params.postId)
     .then(likes => res.json(likes)
     .catch(err => res.status(500).json({msg: "Cannot add a new like"});
};
Collapse
 
d0xzen profile image
D0xzen

Thanks Kryz :D

Yeah at the end i did like that