Adventures in Express
I've recently had the pleasure to begin playwith express, and I'm very impressed with its simplicity and the ease with which extra functionality may be added using middleware. I've created some fairly complicated APIs, both RESTful and GraphQL, without much of a hassle.
What is Express?
Per expressjs.com, Express is a
Fast, unopinionated, minimalist web framework for Node.js
Express provides functionality for building full-stack web applications written in JavaScript on top of Node's built-in HTTP server.
This Series
Over the course of several posts, I intend to build out various application to illustrate the power of express. The goal is to educate myself on these topics. Hopefully it serves you as well!
Part 1: Hello, World!
The source for this walk-through is available on GitHub:
There are several versions of this on the internet, but this the one I've put together from scratch to illustrate how simple building an Express application can be.
Creating the Application
- Create your application folder and initialize your repository and npm package
~$ mkdir express-pt1-hello-world ~$ cd express-pt1-hello-world ~/express-pt1-hello-world$ git init ~/express-pt1-hello-world$ npm init
- Install express
~/express-pt1-hello-world$ npm install --save express
Create index.js in the application's root directory
~/express-pt1-hello-world$ touch index.js
and add the following code
const express = require('express'); const app = express(); app.get('/', (_req, res) => { res.send('Hello, World!'); }); const port = 3000; app.listen(port, () => { console.log(`Listening at http://localhost:${port}`); });
- Run the application
You should see the following output in the console:~/express-pt1-hello-world$ npm run start
and navigating toListening at http://localhost:3000
http://localhost:3000
, you should seeHello, world!
What's going on here?
In this simple example, we've created an application, added 1 dependency, express
, and added some code to start our service. Let's peak at the code in index.js
again, but I'll add some commentary.
const express = require('express');
const app = express();
Here we require the express dependency and create an application object named app
. The application object helps us add functionality to the service through its API.
app.get('/', (_req, res) => {
res.send('Hello, World!');
});
In this case, we're using the get
method to add have the service respond to HTTP GET requests at the path /
with the text Hello, World!
. Note the _req (denoted with an underscore because I'm not using it) and res parameters. These allow us to access information about the incoming HTTP request as well as allow us to manipulate the response. In this case, we're simply sending a text response.
const port = 3000;
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
And finally, this piece of code starts the server and has it listen for requests on port 3000.
Conclusion
You've now seen how quickly a web service can be setup with Express. Over the next few posts, I'll add functionality to this example to fit some basic use cases.