Add and Organize New Routes in an Express App

Share this video with your friends

Send Tweet

This lesson will cover how to add & organize routes in an express app.

We'll create an instance of express.Router() in its own routes/index.js file and use app.use(routes) to include those in our main application. The route handlers will live in their own file routes/notes.js.

The lesson will cover CRUD routes (create, read, update, delete) and go over some of the HTTP verbs supported by the express router.

We will be stubbing the following routes needed to power a note-taking application:

GET /notes
GET /notes/:id
POST /notes
POST /notes/:id
DELETE /notes/:id

The routes won't themselves do anything but return sample data. This approach is often helpful when building a new API, instead of building out the full functionality, first define the shape of the URLs and structure of the responses. Mock data can be used to simulate the response and ensure the routes are going to serve the needs of your application without having to build it all.


More on the verbs supported by HTTP here More on the express routes capabilities here

~ 3 years ago

oldfashioned

~ 3 years ago

thanks