DEV Community

Cover image for Create server using Node JS with Framework Express
Hendra Kusuma for Zetta

Posted on

Create server using Node JS with Framework Express

Hello, come back again with me, this time I want to give you a tutorial on how to create a server using Node JS with the framework Express. this time is different from the previous article. if previously discussed how to create a server natively, now I will explain about creating a server using the Express framework

Background
In this case, currently, I'm on learning Javascript backend, there are 2 ways to native and use the framework Express JS, I want to give a tutorial for those who all read this article

Task
create a server locally with the Javascript language using the framework Express

Getting Started
For the first step, you can use in command line npm init, and you can input information there about your package or your file. that is a starter pack and will add file package.json and there is store all the information that you have entered. After that, you can add file js, for example, you can add server.js. you can install library express on the command line, the command is npm install express. We can see the code below

const express = require('express')

const app = express()

app.get('/', (req, res) => {
  res.send('Hello Handsome!')
})

app.listen(8000, () => {
    console.log('Example app listening on port 8000!')
})
Enter fullscreen mode Exit fullscreen mode

preview on http://localhost:8000/

We can see the improvement in code there is easier to understand and simple for code than using native. You can call the express function using require, and all stored functions on the variable app. Now there is to use the method get you can use: app.get('/', (req, res) => {
res.send('Hello Handsome!')
})

now on framework Express, the method you use is res.send not res.end.

you can make routing on Express, I show you the code below :

const express = require('express')

const app = express()

const auth = 'Bearer 1234'

app.get('/', (req, res) => {
  res.send('Hello Handsome!')
})

app.get('/user', (req, res) => {
    console.log('req.headers.authorization', req.headers.authorization)
    if (req.headers.authorization === auth) {
        res.send({
            name: 'Hendra Kusuma',
            job : 'Programmer',
            age : 33,
            favorite_food : 'Nasi Goreng'
         })
    }   else {
        res.status(401).send('Unauthorized')
    } 

})

app.listen(8000, () => {
    console.log('Example app listening on port 8000!')
})
Enter fullscreen mode Exit fullscreen mode

you can see that, routing can be created using the function 'if' and 'else'. If you look at the routing, it is distinguished by the '/' and '/user' signs after the URL address.

The data type for 'res' can be HTML or JSON text. exemplified in the '/user' routing it is in the form of JSON, and for other than status code 200, for example, 401 the contents are in the form of an unauthorized text file. I have added coding to check authorization, to read the headers it needs a Postman application, and I have tried to check how it looks when using authorize bearer Token in the postman application. The display can be seen below.

Preview Postman

okay, that is the simple ways to make server using Express. thanks for your read, see you in the next post :)

Email : hendrakusuma.vegas@gmail.com
Source code : https://github.com/Hendra-Kusuma/Express-Web

Top comments (0)