Created
July 20, 2018 18:04
-
-
Save chriswhong/11e2b2d68505e239f89f46e36aaa03d2 to your computer and use it in GitHub Desktop.
Convenience Redirect in express.js. Build URLs using a known unique id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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