JavaScript Answers

How to create a basic static file server in Node.js?

Spread the love

To create a basic static file server in Node.js, we use Express.

For instance, we write

const express = require("express");
const app = express();
const port = process.env.PORT || 4000;

app.use(express.static(__dirname + "/public"));

to create a file server by calling express.static to expose the /public folder as a static folder.

We call app.listen to start a web server.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *