Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriswhong/11e2b2d68505e239f89f46e36aaa03d2 to your computer and use it in GitHub Desktop.
Save chriswhong/11e2b2d68505e239f89f46e36aaa03d2 to your computer and use it in GitHub Desktop.
Convenience Redirect in express.js. Build URLs using a known unique id
const express = require('express');
const router = express.Router();
router.get('/:ulurpnumber', async (req, res) => {
const { app, params } = req;
const { ulurpnumber } = params;
// find projectid for this ceqrnumber
// http://localhost:3000/projects/ulurp/170358ZMM
const SQL = `SELECT dcp_name as projectid FROM normalized_projects WHERE ulurpnumbers ILIKE '%${ulurpnumber}%'`;
// ulurpnumber should be 6-10 capital letters, numbers, and hyphens
if (ulurpnumber.match(/^[0-9A-Za-z]{4,12}$/)) {
try {
const { projectid } = await app.db.one(SQL);
const url = `https://zap.planning.nyc.gov/projects/${projectid}`;
res.redirect(301, url);
} catch (error) {
res.redirect(301, 'https://zap.planning.nyc.gov/projects');
}
} else {
res.status(422).send({
error: 'Invalid input',
});
}
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment