Creating a Node.js app with Express

Created by eneaslari 13/8/2023

nodejs

1. Setting Up Your Environment

Ensure you have Node.js and npm (Node Package Manager) installed. If not, you can download them from Node.js official site.

2. Initialize a New Node.js Project

Create a new directory for your project and initialize it.

mkdir my_express_app
cd my_express_app
npm init -y

3. Install Express

Now, install the Express framework and save it to your package.json.

npm install express

4. Writing a Simple Express Server

Create a new file named server.js in your project directory and add the following 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 is running on http://localhost:${PORT}`);
});

This code sets up a simple Express server that responds with "Hello, World!" when someone accesses the root of your server.

5. Run Your Express Server

node server.js

Now, if you visit http://localhost:3000 in your web browser, you should see the message "Hello, World!".

6. Expand Your App (Optional)

  • Middleware: Express has a powerful middleware system. For example, if you want to parse JSON bodies, you would use the express.json() middleware.

  • Routing: You can define routes for different HTTP verbs (GET, POST, etc.) or even define complex routing patterns with parameters.

  • Templates: Express supports various template engines like EJS, Pug, Handlebars, etc., which can be used to render dynamic HTML content.

  • Static Files: Serve static files (e.g., images, CSS, JavaScript) using express.static middleware.

7. Additional Packages (Optional)

Depending on your project's needs, you may want to install additional packages:

  • Body Parser: Previously a part of Express.js, this package allows you to parse incoming request bodies. As of Express 4.16.0+, you can use express.json() and express.urlencoded() for most of your needs.

  • Nodemon: A utility that monitors changes in your source code and restarts your server.

    npm install nodemon --save-dev
    

    Then, in your package.json, you can set up a start script like this:

    "scripts": {
        "start": "nodemon server.js"
    }
    

    Now, instead of node server.js, use npm start, and your server will auto-restart upon code changes.

8. Final Thoughts

Express provides a solid foundation for building web applications and APIs. As your project grows, consider integrating a database, adopting an MVC architecture, or even using Express with other technologies like GraphQL.

Remember, the official Express documentation is an excellent resource, packed with details and examples. Happy coding!

More to read


My New Plan: Making a Mini-Game Every Week
21/2/2025

This week, I realized that working on big projects can sometimes feel slow, and it’s easy to lose motivation. So, I came up with a new plan—I’ll create and release a small game every week!

Making Tic-Tac-Toe and Sudoku in Just a Few Hours
20/2/2025

This week, I decided to take a break from my main game project and create something simple: **a Tic-Tac-Toe and a Sudoku game**.

How I Made My Game Development Workflow Stress-Free
19/2/2025

Game development can be overwhelming, especially when working on multiple tasks without a clear plan. For a while, I was struggling to organize my thoughts and decide what to focus on next

Designing Fun and Engaging Game Levels
18/2/2025

Creating levels for a game sounds simple, but it comes with its own set of challenges.