DEV Community

Cover image for Creating routing API with Express JS
Hendra Kusuma for Zetta

Posted on

Creating routing API with Express JS

Hello everyone, back again with me. Now I want to share again 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 make a routing API using Node JS Express for backend developer for those who all read this article

Task
making a routing in coding

Getting Started

routing has 3 method, it has 'App method', 'chain method', and 'modular method'. I show you in 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')
Enter fullscreen mode Exit fullscreen mode

you can running and check on your browser localhost:3000/, or localhost:3000/user/create, or localhost:3000/user/update. You can check it one by one on Postman. And for Chaining Method, you can see on 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')
}
Enter fullscreen mode Exit fullscreen mode

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

And the next is 'modular method'. In simple terms, this method is to separate the route file javascript, lets say we make new file JS from the main file, let 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)
})
Enter fullscreen mode Exit fullscreen mode

To configuration for 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 with its link function /api. the function of 'app.use' we can use any routing, midelware, or plugin external. And then we calling/import the routes variable which is the file routing JS its 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
Enter fullscreen mode Exit fullscreen mode

if the routing goes well, it will run as it should and if the application is run it using command node main.js run can be checked according to the routing link e.g. localhost:3000/user/update/api/create. If you successfully appear create on the browser page, it means that 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

Top comments (0)