Published on June 12, 2021
Last Revised on June 19, 2021
This post is a guide to setup a simple local HTTP server with Express.
Express is a Fast, unopinionated, minimalist web framework for Node.js. There are many problems that Express can solve. This guide addresses one of them: setting up a bare-bones HTTP server to run locally.
TL;DR - Grab the code from the repo.
This should be a quick one. Let's get to it!
First thing to do is create a working directory. Navigate your file system and find a suitable place to create a Node project. Once found, create a folder that will contain the project and then cd <projectFolder>
into the folder. Next, use NPM to create a package.json
file.
npm init
you will be prompted with a series of configuration questions for your project. For this guide all of the defaults will suffice.
Next we need to add the Express and CORS packages to the project dependencies. CORS is not necessary but can help circumvent CORS-related problems while testing locally.
npm install express cors
The last step is to create a JavaScript file to define, create, and configure the Express app. Create a file named index.js
(or whatever filename you chose as an 'entry point' filename if you did not use the defaults while creating the package.json
file). Add the following code to index.js
.
// index.js
const express = require('express');
const cors = require('cors');
const app = express();
app.get('/', function (req, res) {
res.send('Hello World. CORS-enabled web server listening on port 3000')
});
app.get('/json-example', function (req, res) {
res.json({
msg: 'Hello World. CORS-enabled web server listening on port 3000',
fakeValue: '1234'
})
});
app.use(cors());
app.use(function (req, res, next) {
res.status(404).send("Sorry, that route doesn't exist");
});
app.listen(3000);
Here is a quick overview of what is going on within index.js
. The first 3 lines are importing the express and cors modules. The const app = express();
creates the Express application. The 2 app.get functions are defining available routes that Express is listening for and declaring each function's callback contents. The 2 app.use functions set up the middleware to use the cors library and respond with a 404 response if an unrecognized route is requested. The last line sets the port that Express is going to listen to.
There are other functions Express can be used for. Check out the express.com for more detailed information and guides.
And that is it. Time to test it out.
node index.js
Open your brower to http://localhost:3000. You should see the message "Hello World. CORS-enabled web server listening on port 3000" which is declared in the index.js
file. Similarly, http://localhost:3000/json-example will provide you with the json response in the other route defined.
This guide sets up a bare-bones local http server. It's a great tool to test client-side API requests, create and test behaviors of future serverless functions, and test those responses once they return to your app.