What you need to build a Node backend

A Node.js backend is a server built with JavaScript that handles requests from a website or app, talks to a database, and sends back data. To build one, you need three things: Node.js installed on your computer, a code editor, and a way to manage your project's dependencies.

Node.js is a runtime environment that lets you write server code in JavaScript instead of languages like Python or Java. When you install Node.js, you automatically get npm (Node Package Manager), which is a tool that downloads and organizes code libraries your project needs. A code editor like Visual Studio Code lets you write and organize your files. That's the foundation — everything else builds from there.

Key Takeaways

  • Download and install Node.js from nodejs.org, which includes npm, the package manager you need to add libraries to your project.
  • Create a new folder for your project, run npm init to set up a package.json file, and install Express, the most common framework for building Node backends.
  • Write a basic server file that listens for requests on a specific port, then test it by running the file and visiting localhost in your browser.
  • Add routes to handle different requests — for example, one route that returns user data and another that accepts form submissions.
  • Connect your backend to a database like MongoDB or PostgreSQL so your server can store and retrieve information.

Install Node.js and set up your project folder

Go to nodejs.org and download the LTS (Long Term Support) version for your operating system — Windows, Mac, or Linux. Run the installer and follow the prompts. When it finishes, open a terminal or command prompt and type node --version and npm --version to confirm both installed correctly. You should see version numbers for each.

Create a new folder on your computer where your backend code will live. Open a terminal, navigate to that folder, and run npm init. This command asks you questions about your project — name, description, entry point — and creates a file called package.json. This file tracks all the libraries your project uses and the scripts you run. You can press Enter to accept the defaults for most questions.

Install Express and create your first server file

Express is a framework that makes it much easier to build a Node backend. It handles routing (directing requests to the right code), middleware (code that runs on every request), and sending responses back to the client. Run npm install express in your terminal. npm downloads Express and adds it to a folder called node_modules, and records it in package.json so anyone else working on your project knows it's needed.

Create a new file in your project folder called server.js or index.js. Open it in your code editor and write this code:

const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => {   res.send('Hello World'); }); app.listen(port, () => {   console.log(`Server running on http://localhost:${port}`); });

This code imports Express, creates an app, and tells it to listen for requests on port 3000. The app.get('/', ...) line creates a route — when someone visits the root URL, the server sends back "Hello World". Save the file, then run node server.js in your terminal. You should see "Server running on http://localhost:3000". Open that URL in your browser and you'll see your message.

Add routes to handle different requests

A route is a path on your server that does something specific. You've already created one route that responds to GET requests at the root. Most backends need multiple routes — one to fetch user data, another to create a new user, another to delete something. Each route is a different path and a different action.

Add these routes to your server.js file, before the app.listen() line:

app.get('/users', (req, res) => {   res.json({ users: ['Alice', 'Bob', 'Charlie'] }); }); app.post('/users', (req, res) => {   res.json({ message: 'User created' }); });

The first route responds to GET requests at /users and sends back a JSON object with a list of users. The second route responds to POST requests at /users and sends back a confirmation message. GET is for retrieving data; POST is for sending data to the server. Save your file, stop the server (Ctrl+C), run node server.js again, and visit http://localhost:3000/users in your browser. You'll see the JSON response.

Parse incoming data with middleware

When a client sends data to your server — like form information or JSON from a frontend app — your server needs to understand it. Express middleware is code that runs on every request and can transform or check that data. The most common middleware for Node backends is the JSON parser, which reads incoming JSON and makes it available to your routes.

Add this line near the top of your server.js file, after const app = express();:

app.use(express.json());

Now your routes can receive JSON data. Modify your POST route to use that data:

app.post('/users', (req, res) => {   const newUser = req.body.name;   res.json({ message: `User ${newUser} created` }); });

When a client sends a POST request with JSON like { "name": "David" }, your route receives it in req.body and can use it. This is how your backend receives information from forms, mobile apps, or other services.

Connect to a database

A backend without a database can only send back the same hardcoded data every time. To store and retrieve real information — user accounts, posts, products — you need a database. The two most common choices for Node backends are MongoDB (a document database) and PostgreSQL (a relational database).

MongoDB is simpler to start with because it stores data as JSON-like objects, which feels natural in JavaScript. PostgreSQL is more structured and better for complex relationships between data. For a first project, MongoDB is often easier. Install the MongoDB driver with npm install mongodb, then add this code to connect to a local MongoDB database:

const { MongoClient } = require('mongodb'); const url = 'mongodb://localhost:27017'; const client = new MongoClient(url); client.connect();

This assumes MongoDB is running on your computer. If you don't have MongoDB installed, you can use MongoDB Atlas, a cloud service that hosts databases for free. Sign up at mongodb.com/cloud/atlas, create a cluster, and use the connection string they provide instead of the localhost URL. Once connected, you can write data to the database in your routes and retrieve it when clients request it.

Test your backend and prepare for deployment

Before you put your backend online, test it thoroughly. Use a tool like Postman or Insomnia to send requests to your routes and check the responses. Postman is free and lets you create GET, POST, PUT, and DELETE requests without writing code. Send data to your routes, verify the database stores it, and make sure error cases are handled — what happens if someone sends invalid data or requests something that doesn't exist.

When you're ready to put your backend online, you'll deploy it to a hosting service. Popular choices include Heroku, Railway, Render, and AWS. Most of these services read your package.json file, install your dependencies, and run your server. Before deploying, add a start script to your package.json file by adding this line inside the "scripts" section:

"start": "node server.js"

This tells the hosting service how to start your server. Each hosting platform has different steps for deployment, but they all follow the same pattern: connect your code repository, set environment variables (like database URLs), and the service handles the rest.

Frequently Asked Questions

What's the difference between GET, POST, PUT, and DELETE?

GET retrieves data without changing anything. POST sends new data to create something. PUT updates existing data. DELETE removes data. These are the four main HTTP methods your backend will handle. Most simple backends start with GET and POST, then add PUT and DELETE as they grow.

Do I need to use Express, or can I build a Node backend without it?

You can build a backend with just Node's built-in http module, but Express saves you hundreds of lines of code. It handles routing, middleware, and common tasks automatically. For any real project, Express or a similar framework is the standard choice.

How do I keep my database password secret?

Never put passwords or API keys directly in your code. Use environment variables — create a file called .env, store sensitive information there, and load it with a package called dotenv. Add .env to your .gitignore file so it never gets uploaded to version control. This way, each environment (your computer, a server) can have different credentials.

What should I do if my server crashes or stops responding?

During development, restart it by pressing Ctrl+C and running node server.js again. In production, use a process manager like PM2 that automatically restarts your server if it crashes. Most hosting services handle this for you automatically.

Can I build a frontend and backend in the same Node project?

Yes, but it's cleaner to keep them separate. Your backend serves data as JSON, and your frontend (built with React, Vue, or plain HTML) requests that data. This separation makes it easier to test, deploy, and scale each part independently.