This project shows some basic usage of Express.js features, including:
- handling incoming HTTP requests using express routes
- exploring the purpose of middleware and how it works
- debugging express apps using morgan and Postman
- handling data included in HTTP POST requests
- handling file uploads using multer
- making requests from express to external APIs with axios and serving the data to the client
- setting environmental variables using dotenv
- use of a
.gitignore
file to exclude 3rd-party code and autogenerated files from version control tracking - use of a
package.json
with npm to track dependencies and streamline installation.
Many of the features of Express exhibited in this project are explained at a high level in this prepared lecture presentation.
There are some common practices that we have not included here, since we believe they are distraction's from a beginner's ability to master Express.js' code patterns.
- exclusion of any front-end code
- exclusion of user account registration or log in functionality
- exclusion of unit testing modules, like mocha and chai
- exclusion of any database
- exclusion of express-router, which would allow us to better organize large collections of routes
In the project directory, you can run:
-
npm install
- installs all the dependencies listed in thepackage.json
configuration file. This is necessary before running the app, since the 3rd-party dependency code is excluded from version control by the.gitignore
git settings file. -
npm start
- starts the server the old-fashioned way. When started in this way, the server has to be manually restarted whenever you change the code. -
nodemon server
- starts up the server with a monitoring process that will stop and restart the server automatically anytime there is a code change. If you haven't yet done so, installnodemon
globally withnpm install -g nodemon
.
Once started, the server will by default be available on port 3000
of the local computer at the URL, http://localhost:3000
.
This server code sets up routes for several API end-points - URLs to which a client can make requests. Except where indicated, the server accepts GET
requests to these routes.
The example routes include:
/
- a route for the "root" document - usually the home page, but in this case a simple text response./html-example
- a route that shows how to return a simple HTML document./static/css/main.css
- a route that servers as an example of servnig a static file that does not need dynamic server logic./json-example
- a route that shows how the server can respond with a JSON document./middleware-example
- a route that exhibits the concept of middleware./post-example
- a route that acceptsPOST
requests with some content the server expects the client to send in the body of the request./upload-example
- a route that accepts aPOST
request with one or more files in the request that the client uploads to the server./proxy-example
- a route that causes the server to make a request to a different API and then respond to the client with the data received from that API./dotenv-example
- a route that shows how a server can use thedotenv
module to load environmental variables from a file named.env
into Javascript code./parameter-example/22
- a route that shows an example of how parts of the route URL - in this case the number22
- can be parameterized and accessed in code.
It is possible to try out the routes directly in a web browser. However, it is often more convenient to use an API-testing tool like Postman to test out the server API end-points.
- Import the test file named
express-js-starter-app.postman_collection.json
into Postman to test out the API end-points.