support@codebucket.net

Learn some important functions in ExpressJs

Learn some important functions in ExpressJs

K. M. Shawkat Zamil | October 17, 2022

Hello Developers,

In this tutorial, I will show you some basic and important functions in ExpressJs. First, you have to install NodeJs in your working environment. Follow the tutorial: (Tutorial Link)

 

First, create a folder and open the command prompt. Then run the below command: 

 

npm init --y

 

Then create a file named index.js. After that paste the below code into the index.js file.

Run the below command first.

 

npm install express --save

 

1. Get any string in ExpressJs

 

The first method is end() and send(). The send() method represents the response body. The end() function represents the final state.

 

/index.js

var express = require('express');

app = express();

app.get("/response", function name(request, response) {
    
    /**
     * response.send()-> represent body
     * response.end()-> represent the final state
    */
    
    //response.send("Hello Express Js");
    response.end("Hello Express Js");
})

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

2. Get Status Code in ExpressJs

 

The second method is status(). In the status() method, you can write the status code. Learn more Status Code

 

/index.js

var express = require('express');

app = express();

//response status code
app.get("/statuscode", function name(request, response) {
   
    response.status(401).end("UnAuthorize");
})   

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

3. Use json() method in ExpressJs

 

The third method is json(). In the json() method, a JSON object should be passed.

 

/index.js

var express = require('express');

app = express();

//response show json
app.get("/responseNew", function name(request, response) {
   
    let myJSON = [
        {
            name: "Test 1",
            roll: 01
        },
        {
            name: "Test 2",
            roll: 02
        }
    ];

    response.json(myJSON);
    
})   

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

4. Download any file in ExpressJs

 

The fourth method is download(). In the download()method, pass a file location.

 

/index.js

var express = require('express');

app = express();

//response download
app.get("/responseDownload", function name(request, response) {
    response.download('./uploads/code.png');
})  

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

5. Use redirect() function in ExpressJs

 

The fifth method is redirect(). In the redirect() method, an URL should be passed.

 

/index.js

var express = require('express');

app = express();

//redirect route
app.get("/redirect", function name(request, response) {
    response.redirect('http://localhost:8000/responseNew');
   
}) 

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

6. Use append() function in ExpressJs

 

The sixth method is append(). In the append() method, you can set any value in the header of the response.

 

/index.js

var express = require('express');

app = express();

//response header
app.get("/header", function name(request, response) {
    
    /**
     * append(key,value)
    */
    response.append("name","Zamil");
    response.append("city","Dhaka");
    
    response.status(201).end('Hello Worlds');
})

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

7. Use cookie() function in ExpressJs

 

The seventh method is cookie(). In the cookie() method, you can set anything in the browser. To save any cookie use key and value in cookie() method.

 

/index.js

var express = require('express');

app = express();

//redirect route
app.get("/redirect", function name(request, response) {
    response.redirect('http://localhost:8000/responseNew');
   
}) 

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

8. Use clearCookie() function in ExpressJs

 

The seventh method is clearCookie(). In the clearCookie() method, you can remove any cookie from the browser. To remove any cookie use key in clearCookie() method.

 

/index.js

var express = require('express');

app = express();

//clear cookie
app.get("/cookieClear", function name(request, response) {
    
    /**
     * clearCookie(key)
     */

    response.clearCookie("name");
    response.clearCookie("city");

    response.end("Cookie Clear Success");
})

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

After all that, we call the listen() method to set the port. After that run the below command in the command line.

 

node index.js

 

Run the below line in the browser, the end-point represents the parameter created in the app.get("/end-point") method.

 

http://localhost:8000/{end-point}

 

Hope this might help in the development journey.

Read More: Run the first application in ExpressJs

K. M. Shawkat Zamil

K. M. Shawkat Zamil

Senior Software Engineer

I am a Senior Software Engineer in a reputed company in Bangladesh. I am a big fan of Laravel, PHP Programming, MSSQL Server, MySql, JavaScript, and lots more. I love to help people who are especially eager to learn. I believe in patience and motivation.