Routing

Newgate provides an Express-like routing system that supports HTTP methods, route parameters, wildcards, and query strings.

Basic Routing

Routes are defined using methods on the app instance that correspond to HTTP verbs.

import App from 'newgatejs';
const app = new App();

// GET method
app.get('/', (req, res) => {
  res.send('Hello World');
});

// POST method
app.post('/', (req, res) => {
  res.send('Got a POST request');
});

// PUT method
app.put('/user', (req, res) => {
  res.send('Got a PUT request at /user');
});

// DELETE method
app.delete('/user', (req, res) => {
  res.send('Got a DELETE request at /user');
});

Route Parameters

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object.

app.get('/users/:userId/books/:bookId', (req, res) => {
  res.json(req.params);
});
// Request URL: http://localhost:3000/users/34/books/8989
// req.params: { "userId": "34", "bookId": "8989" }

Hyphen and Dot

Parameters can be separated by hyphens (-) and dots (.).

app.get('/flights/:from-:to', (req, res) => {
  res.json(req.params);
});
// /flights/LAX-SFO => { "from": "LAX", "to": "SFO" }

app.get('/plantae/:genus.:species', (req, res) => {
  res.json(req.params);
});
// /plantae/Prunus.persica => { "genus": "Prunus", "species": "persica" }

Wildcard Routes

You can use the asterisk * as a wildcard to match any string.

app.get('/files/*', (req, res) => {
  // Matches /files/hello.txt, /files/nested/path/file.jpg, etc.
  res.json({ path: req.url });
});

Query Strings

Query string parameters are automatically parsed and available in req.query.

app.get('/search', (req, res) => {
  const { q, limit } = req.query;
  res.json({ query: q, limit });
});
// GET /search?q=test&limit=10 
// req.query: { "q": "test", "limit": "10" }

Route Handlers

You can provide multiple callback functions that behave like middleware to handle a request.

app.get('/example/b', 
  (req, res, next) => {
    console.log('the response will be sent by the next function ...');
    next();
  }, 
  (req, res) => {
    res.send('Hello from B!');
  }
);
Last updated: 12/11/2025 Edit on GitHub