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.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

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

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay