Create a Node.js Express MVC application with Routes in minutes

Posted by

Node.js Express is a powerful web application framework for building web applications in Node.js. It provides a robust set of features for building web applications, including routing, middleware support, and template engines. In this tutorial, we will go over the basics of Node.js Express MVC and routes in minutes.

To get started, make sure you have Node.js installed on your computer. You can download it from the official Node.js website. Once Node.js is installed, you can create a new project folder and open it in your favorite code editor.

Step 1: Setting up the project folder
Create a new project folder for your Node.js Express application. Open the folder in your code editor and create a new file called app.js. This file will be the entry point for your application.

Step 2: Installing Express
Next, you need to install the Express framework. Open a terminal window and navigate to your project folder. Run the following command to install Express:

npm install express

Step 3: Creating the Express application
In your app.js file, require the Express module and create a new Express application instance:

const express = require('express');
const app = express();

Step 4: Setting up routes
Now that you have created an Express application, you can define routes to handle different HTTP requests. Routes are URL patterns that map to specific functions in your application.

To create a route that responds to a GET request, use the app.get() method:

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

This route will respond with the text ‘Hello, World!’ when the root URL is accessed.

Step 5: MVC in Express
Model-View-Controller (MVC) is a design pattern that separates an application into three main components: models, views, and controllers. Express does not have built-in support for MVC, but you can implement it using separate files and folders for each component.

Create a new folder called controllers in your project folder. Inside this folder, create a new file called homeController.js. This file will contain the controller logic for handling requests to the home route.

// homeController.js

exports.home = (req, res) => {
  res.send('Hello, MVC!');
};

Next, require the homeController.js file in your app.js and use it to define a new route:

const homeController = require('./controllers/homeController');

app.get('/mvc', homeController.home);

This route will respond with ‘Hello, MVC!’ when the /mvc URL is accessed.

Step 6: Running the Express application
To run your Express application, add the following code to the end of your app.js file:

const port = 3000;

app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});

Finally, open a terminal window, navigate to your project folder, and run the following command to start your Express application:

node app.js

You should see a message in the terminal indicating that the server is running on http://localhost:3000. Open a web browser and navigate to http://localhost:3000 to see your Express application in action.

Congratulations! You have successfully set up a Node.js Express application with MVC and routes in minutes. Happy coding!