DEV Community

Abdo
Abdo

Posted on

How to make basic express api

ExpressJS is a JavaScript framework that allows you to make advanced api for your web app

first you need to make a my-projectfolder

Then open terminal and type this code

npm init -y
Enter fullscreen mode Exit fullscreen mode

Then you need to install express, you can install express by this code

npm i express
Enter fullscreen mode Exit fullscreen mode

After this code you need to replace scripts in your package.json file with this code

"scripts":{
  "start":"node app.js"
}
Enter fullscreen mode Exit fullscreen mode

Then make app.js file
and pus this code inside your app.js file

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

// /api route
app.get('/api',(req, res) => {
    res.send('hello world!') // this will return hello world! When you go to https://localhost:3000/api
})

// make app listen on port 3000
app.listen(3000, (req, res) => {
  console.log("app listening on https://localhost:3000")
})
Enter fullscreen mode Exit fullscreen mode

then run

npm start
Enter fullscreen mode Exit fullscreen mode

And navigate to https://localhost/3000/api
And you'll see 'hello world!'
Text
That's it.

Top comments (0)