DEV Community

sonicx180
sonicx180

Posted on

Create an Express Server

How to Create an Express Server.

I recommend this only for beginners since it's so basic.

Step 1:
Create a folder and name it whatever you like.
Go to your terminal and run (cd folder name likecd myapp )and run npm init -y

Go to a text editor (like VSC or Caret or Sublime Editor) and open the index.js file

Then write this in your file:

const express = require("express");
const app = express();
Enter fullscreen mode Exit fullscreen mode

The first line of code imports the express module.
The second line of code initializes an express app.

But the app isn't really running yet.

Write these lines of code after the code above.

app.get('/', (req,res) => {
 res.send("Hello World!")
}
app.listen(3000, (err) => {
if (err) console.log(err)
else console.log("Server Running on port 3000")
}
Enter fullscreen mode Exit fullscreen mode

Now, to break this down.

For the first three lines of code, on the homepage (like https://dev.to) it will show Hello World!. By the way, read about arrow functions here https://www.w3schools.com/js/js_es6.asp. req stands for request and res stands for response.
As for the rest of the code, the app.listen function makes the app listen on port 3000 and there is an error logger too.

Now go to your terminal and run cd (your folder name) like cd myapp. After that run npm i express. Then run node index.js. Your app should be up and running !

If this doesn't express much, go to Free Code Camp back end development or go to Express Js.

Well, that's about it. Comment if you have any suggestions. Thanks for reading!

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay