Skip to content

A straightforward exhibition of some core features and best practices of using express.js.

License

Notifications You must be signed in to change notification settings

nyu-software-engineering/express-js-starter-app

 
 

Repository files navigation

Express.js Starter App

Concept

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.

Intentional exclusions

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

Available Scripts

In the project directory, you can run:

  • npm install - installs all the dependencies listed in the package.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, install nodemon globally with npm 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.

Server API End-Points

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 accepts POST requests with some content the server expects the client to send in the body of the request.
  • /upload-example - a route that accepts a POST 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 the dotenv 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 number 22 - can be parameterized and accessed in code.

Running the server

Testing the routes

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.

About

A straightforward exhibition of some core features and best practices of using express.js.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 95.3%
  • HTML 3.9%
  • CSS 0.8%