Build a local server With Node.js and Express

Build a local server With Node.js and Express

Node makes it extremely easy for developers to spin up a local server for web development. This tutorial uses the Express middleware framework to create a bare bone web app running on a local Node server

Steps

This tutorial assumes you are starting from scratch. It includes:

  1. Installing Node.js
  2. Creating a Node.js project
  3. Adding the Express.js module
  4. Launching the app

Installing Node.js

The first step will be installing the Node.js framework on your machine. Visit the official node.js and download the appropriate version for you.

Follow the installation wizard. Once complete, run the following:

node --version

If Node has been successfully installed, you should see something like:

v4.2.3

Creating a Node.js project

Using the command line, navigate to your home directory and create a new folder called ‘myproject’:

cd ~
mkdir myproject cd myproject

Now that you have an empty folder, you are ready to initialize a new node.js project. Every Node.js project requires a package.json file at it’s root. This file defines meta data related information (author, description) as well as dependencies for the project.

While you can simply create a package.json file yourself, node’s package manager (npm) makes this even easier. Simply run:

npm init

You will be prompted to enter information such as author, description for the project. Once complete, you will now see the generated package.json file in your project’s root directory.

Open your new package.json file and have a look. You will see a “main” attribute, which should point to an index.js file. This is the main script your server will run on initialization.

To create this main script file, run:

touch index.js

Adding the Express.js module

In order for the real web development to begin, you need to set up a web server for your project. While you could use node’s https module to build out basic routes, the Express framework makes running a web server even easier. Express is part of the popular MEAN stack, and allows for simplified routing. It’s easily configurable. Open your index.js file in your favorite text editor, and paste:


//Require Express module
const express = require('express');
//Define port for local web server
let port = 5000;
//Initialize your express app
const app = express();
//Create simple get route for the index of your app
app.get('/', function(req,res){
res.send('Hello World');
})
//Listen on the defined port
app.listen(port, function(){
console.log('Listening on port 5000')
})

This defines the initialization script for your Node.js app.

Launching the app

To launch the server, navigate to the root directory of your project and run:

node index

If all goes according to plan, you should see something like:

Listening on port 5000

Finally, check your browser at** localhost:5000 **and you should see ‘Hello World!’

Congratulations! You are now up and running. In just a few short steps, you have a web server running on Node!

nodejs node javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Node canvas is a Cairo backed Canvas implementation for NodeJS.

node-canvas is a Cairo-backed Canvas implementation for Node.js.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.

How to get started Internationalization in JavaScript with NodeJS

Tutorial showing how to use the Intl JS API in NodeJS (i18n). We'll install a module to unlock the Intl API languages for Node and test out RelativeTimeFormat to translate and localise relative times in JavaScript.