How to Create routing API with Express JS

hendra kusuma
Zetta Tech
Published in
3 min readFeb 19, 2023
Photo by Raditya Putra Mahardika on Unsplash

Hello everyone, back again with me. Now I want to share about creating routing API with express JS. This article continues the previous article so make sure you have read the previous article.

Background
In this case, currently, I’m on learning Javascript backend, I want to give a tutorial about making a routing API using Node JS Express for backend developers for those who all read this article

Task
making a routing in coding

Getting Started

routing has 3 methods, it has 'App method', 'chain method', and 'modular method'. I will show you the code.

express = require('express')
app = express()
//this is a 'APP METHOD'
app.get(`/`, (req, res) => {
res.send('Hello World!')
})

// /user/create
app.post('/create', (req, res) => {
res.send('Create Order!')
})

// /user/update
app.put('/update', (req, res) => {
res.send('Update Order')
})

app.listen(3000, () => {
console.log('server is running on port 3000')

you can run and check on your browser localhost:3000/, or, or localhost:3000/user/update. You can check them one by one on Postman. And for Chaining Method, you can see below.

// this is 'CHAINING METHOD'
app.route('/product')
.get(req, res)) => {
res.send('get a route /product')
}
.post(req, res)) => {
res.send('post a route /product')
}
.put(req, res)) => {
res.send('put a route /product')
}
.delete(req, res)) => {
res.send('delete a route /product')
}

The chaining method looks almost the same but for the initial invocation use app.route, it remains only to call 'get' 'post', 'put', and delete.

And the next is the 'modular method'. In simple terms, this method is to separate the route file javascript, let's say we make a new file JS from the main file, let's say we name it "mainFile.JS". so the route code is not combined with the main file, this functions to make our files more tidy and structured. let's see in the code

const express = require('express')
const app = express()
const routes = require('router')
const router = express.Router()

// Routing 'MODULAR METHOD'
app.use('/api', (routes(router)

app.listen(8000, () => {
console.log('Server is running on port', 8000)
})

To configure the router in express, There we call the default function express module, namely const router = express.Router(). We can see there how the routing module is called by using app.use its link function /api. the function of app.usewe can use any routing, middleware, or plugin external. And then we call/import the routes variable which is the file routing JS it has been called outside the main file JS by using require, so we can create a new file for routing example 'routes.js', let's code the contents in routes.js.

module.exports = function (router) {
router.get('/', function (req, res) {
res.send('Routes Modular!')
})

router.post(`/create`, function (req, res) {
res.send('create')
})

return router

if the routing goes well, it will run as it should, and if the application is run it uses a command node main.js run that can be checked according to the routing link e.g. localhost:3000/user/update/api/create. If you successfully appear to create on the browser page, you have succeeded in using the modular method.

okay thanks for your read, see you in the next post :)

Github :https://github.com/Hendra-Kusuma
Email: hendrakusuma.vegas@gmail.com

--

--