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


Designing Fun and Engaging Levels for My Space-Themed Match-3 Game
8/2/2025

Designing levels for my space-themed match-3 game has been a journey of balancing **fun, challenge, and variety**. In this article, I share my experience with **creating engaging puzzles, managing difficulty, and keeping gameplay fresh**. From playtesting to strategic layouts, these insights shape my approach to making levels that players will love. 🚀

From Burnout to Balance: Rediscovering Energy in Game Dev, Fitness, and Life
7/2/2025

A reflection on how burnout, illness, and lost momentum forced me to rethink my approach to productivity, motivation, and balance. Now, I’m refactoring my life—one habit at a time—to rebuild my energy without falling into the same cycle again.

New Year, New Code: My Developer Resolutions for the Year
29/12/2024

In this blog post, I share my New Year's resolutions as a developer: posting more about my work on social media, writing more articles, and finally finishing and publishing a game. Plus, I offer practical tips on setting realistic and achievable goals to help fellow developers kick off the year with purpose!

Bad Practices in JavaScript: Common Pitfalls and How to Avoid Them 🚫
28/12/2024

JavaScript is like the Swiss Army knife of web development—it’s everywhere, from tiny website features to massive, complex web apps. But with great power comes... well, the chance to make a mess! Its flexibility and loose rules make it super easy to use, but also super easy to misuse.