Handle unknown routes

This commit is contained in:
Marcus Noble 2018-01-14 12:41:55 +00:00
parent 6d6235d267
commit 5e5d76d8fb

View File

@ -34,11 +34,22 @@ const getProfileURLs = async handle => {
}); });
}; };
server.use(restify.plugins.queryParser()); const handleResponse = (profileURLs, req, res) => {
if (req.getContentType() === 'application/json') {
return res.send(profileURLs);
} else {
const imageURL = profileURLs[req.query.size] || profileURLs.original;
// Handle some stuff we dont have request
server.get('favicon.ico', (req, res) => res.send(404)); .get(imageURL)
server.get('sw.js', (req, res) => res.send(404)); .on('error', function(err) {
console.log(err);
})
.pipe(res);
}
};
server.use(restify.plugins.queryParser());
server.get('/:handle', async function (req, res) { server.get('/:handle', async function (req, res) {
if (!req.params.handle) { if (!req.params.handle) {
@ -55,18 +66,15 @@ server.get('/:handle', async function (req, res) {
}; };
} }
if (req.getContentType() === 'application/json') { return handleResponse(profileURLs, req, res);
return res.send(profileURLs); });
} else {
const imageURL = profileURLs[req.query.size] || profileURLs.original;
request server.get('/.+', function (req, res) {
.get(imageURL) let profileURLs = {
.on('error', function(err) { original: 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png'
console.log(err); };
})
.pipe(res); return handleResponse(profileURLs, req, res);
}
}); });
server.listen(process.env.PORT || 9090, function () { server.listen(process.env.PORT || 9090, function () {