How to Write the first express app

How to Write the first express app

Jul 3, 2022·

2 min read

How to create a first express application (rest) ?

What should you have to get started 1.Nodejs download -a runtime for JavaScript outside browser 2.your code editor ex .vs code 3.Platform (windows,mac,linux)

First you need to create a folder either using terminal or using rightclick

mkdir express-tutorial

cd express-tutorial code .

code . -by writting code space .(dot) your vs code will open

either npm or yarn

npm init -y or yarn add -y

It will initialize package.json file where it will helps us to install packages or libraries so that we can start working.

After all this stuff we need to install express

yarn add express npm install express

We only want to install it for development purpose on our folder only not to global whole system

Once express is installed we are ready to go

Create a file named index.js


import express from 'express'
//imported express from express using es6 module

const app = express();
// process.env.PORT means it will check to .env file if we have created and get that file and use it if not then it will use 3000 as port
const port = process.env.PORT || "3000";

// routing

app.get("/", (req, res) => {
  console.log("you are at the home page");
  res.send("home page");
// anymethod(get,post....) takes two arguments one is routes and another is callback()

// the server need to listen

app.listen(port, () => {
  console.log(`server running at the port:http://localhost:${port}`);

Warms Regards From myself to you reader :)

_Happy learning :)