install express dependency with npm :
npm instal express --saveor using yarn :
yarn add expressRouting is basically matches the functions you wrote with the requests that server gets.
This project structure will be like this :
.
+-- routes
| +- users.js
+-- app.js
+-- package.json
+-- yarn.lock
+-- README.md
routes folder is placed by route file, for example is users / category / or anything. that folder is handle route whatever the http method.
app.get('/', (req, res) => { /* */ })
app.post('/', (req, res) => { /* */ })
app.put('/', (req, res) => { /* */ })
app.delete('/', (req, res) => { /* */ })
app.patch('/', (req, res) => { /* */ })Those methods accept a callback function, which is called when a request is started, and we need to handle it.
(req, res) => res.send("Hello World")Express sends us two objects in this callback, which we called req and res , that represent the Request and the Response objects.
const express = require("express");
const router = express.Router();Declare and initialize express router
How to route ? Here is some code :
router.get("/hello", (req, res) => {
res.status(200).send({
msg: "HALLO DUNIA"
});
});and the bottom of the route file :
module.exports = router;and the last part is use in app.js or server.js
app.use("/api/v1", require("./routes/users"));