Routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
Each route can have one or more handler functions, which are executed when the route is matched.
Route definition takes the following structure.
  • app is an instance of express.
  • METHOD is an HTTP request method, in lowercase.
  • PATH is a path on the server.
  • HANDLER is the function executed when the route is matched.
import express from 'express';

const app = express();
app.METHOD(PATH, HANDLER)
const express = require('express');

const app = express();
app.METHOD(PATH, HANDLER)

Basic Routing

All routes are defined in your route files, which are located in the routes directory.
These files are automatically mapped by Express Sweet to the route files specified in the URI and the route handlers defined in the route files.
The following examples illustrate defining simple routes.
routes/user.js responds to GET /user requests.
import {Router} from 'express';
const router = Router();

router.get('/', (req, res) => {
  res.send('Hello World');
});
export default router;
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
  res.send('Hello World');
});
module.exports = router;

Nested Routing

The router supports nested files.
If you create a nested folder structure files will be automatically routed in the same way still.
routes/api/users.js responds to GET /api/users requests.
import {Router} from 'express';
const router = Router();

router.get('/', (req, res) => {
  res.send('Hello World');
});
export default router;
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
  res.send('Hello World');
});
module.exports = router;

Default Routing

Express Sweet can be told to load a default router when a URI is not present, as will be the case when only your site root URL (/) is requested.
To specify a default router, open your config/config.js file and set this variable:
default_router: '/blog'
Where blog is the name of the router module you want used.
Next, create the routes/blog.js module.
import {Router} from 'express';
const router = Router();

router.get('/', (req, res) => {
  res.send('Hello World');
});
export default router;
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
  res.send('Hello World');
});
module.exports = router;
Now when you request the root URL (/), you will see "Hello World".

Routing Methods

Express supports the following routing methods corresponding to the HTTP methods of the same names.
For more details about routing, see the Exprees’s Routing Guide.
  • checkout
  • copy
  • delete
  • get
  • head
  • lock
  • merge
  • mkactivity
  • mkcol
  • move
  • m-search
  • notify
  • options
  • patch
  • post
  • purge
  • put
  • report
  • search
  • subscribe
  • trace
  • unlock
  • unsubscribe