How to Create your Own Express Server
Tags: express, jslibrary, server
Published: 2021-02-21 10:38:32

Hey Guys!

Want to know how to create your own Server with Express?

Well, Let's get started!

1. First, fire up a terminal and create a directory. If you don't know how to access the terminal, check out my other <a href = "https://dev.to/code2rithik/how-to-install-hyper-terminal-for-macbook-8db">blog</a>:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/biacwwp44tig9cxp7qew.png)

2. Next, go to the <a href = https://www.npmjs.com/> NPM Website</a> and open this package called Express. Here is the <a href="https://www.npmjs.com/package/express">link</a>: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9y9c6fjhrpjnu4yleq9f.png)

3. Next, fire up your terminal and type <code>npm install express</code> to install the package. It will go through a quick process and it will install:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4d3ff91c6bvxuxy4wa15.png)


4. Then, open your code editor and create server.js:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpd3kku24zk2t24wl93o.png)

5. Now, create a constant called express require it:
```js
//jshint esversion:6

const express = require("express");
const app = express(); 
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hp04a9y3nkbzhazhm54.png)

Now, use the app.get method:
```js
app.get("/", function (request, response) {
   response.send();
});
```
The app. get() method specifies a callback function that will be invoked whenever there is an HTTP GET request with a path ( '/' ) relative to the site root:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvm53mc47ek8voij9mfo.png)

Then, use the app.listen method:
```js
app.listen(3000, function (){
    console.log("Server started on Port 3000");
});
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wt4cwd3vx8loeyxisqks.png)

The app. listen() function is used to bind and listen the connections on the specified host and port.

Now In the app.get method, in respond.send, you can add strings, HTML code and many more.

For this tutorial I will be adding HTML Code. So you can write something like this:

```js
response.send("<h1>This is a heading</h1> 
<h2>This is a heading</h2> 
<h3>This is a heading</h3> 
<h4>This is a heading</h4> 
<h5>This is a heading</h5> 
<h6>This is a heading</h6>");
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qcwlob53c22hqoz3ghz.png)

Now, you can open up your terminal again, make sure you are in the express server directory and type <code>node server.js<code> (Installing Node is required):
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sn4dafm5uf0qone6e7n7.png)

Finally, go to your browser and type <code>localhost:3000</code>  and VOILA you can see the content. This means you have successfully created an express server:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hy783i29swr41h16fejg.png)

If you like this blog then do like it and comment down below.

If you have any questions, do reach out to me in the comments.

Thank You and stay tuned for blogs about our Best Friend Coding!

Remember...

## Keep Coding Y'All👨🏻‍💻