Results

How to Make a Website Using Express.js

By Luke McCrea -


Express.js is a Node.js web app framework that allows you to create web servers using Node.js. It can be used for many different purposes ranging from creating your own API, to hosting your own personal website. In this tutorial, I ‘m going to show you how to create your own website using Node.js and Express.js.

Setup

Make sure that you have Node.js installed on your computer, then create a directory for you application.

Now, we need to install Express.

$ npm install express

After you have done this, create a file named index.js . This is where we will write our code.

Finally, create a folder named public . This is where we will keep the files for our website.

Creating a Web Server

Now that we have everything setup, let ‘s write some code.

First, we will need to import the Express.js module.

const express = require("express");

Now we can create an app.

const app = express();

Now we have an app, but it doesn ‘t do anything. Let ‘s make it so it uses the public directory for the website. We can do this by using app.use() .

app.use (express.static('public'));

Finally, add an index.html file to the public directory. Add some text to the index.html file such as “Hello World “.

Your code should look like this:

const express = require("express");  
const app = express();
app.use(express.static('public'));

Now, if we navigate to localhost:3000 in your browser, you should see your index.html file!