Allow routes to use app.route()
Concerns were raised that the previous method potentially made middleware more complicated. In order to allay this, hoist the full route into the route file. Still a single route per file, but facilitates simpler middleware.
This commit is contained in:
parent
dd5499a21d
commit
58adbdc459
3 changed files with 56 additions and 51 deletions
5
app.js
5
app.js
|
@ -11,7 +11,7 @@ const { match: createPathMatch } = require('path-to-regexp');
|
|||
dot: true,
|
||||
});
|
||||
const pathMatches = [];
|
||||
app.use((req, res, next) => {
|
||||
app.use((req, _res, next) => {
|
||||
const requestUrl = new URL(req.url, 'https://example.com/');
|
||||
let candidateUrl = '';
|
||||
let secondCandidateUrl = '';
|
||||
|
@ -53,6 +53,9 @@ const { match: createPathMatch } = require('path-to-regexp');
|
|||
if ( routeObj.post ) {
|
||||
app.post(`/${route}`, routeObj.post);
|
||||
}
|
||||
if ( routeObj.route ) {
|
||||
routeObj.route(app.route(`/${route}`));
|
||||
}
|
||||
}
|
||||
app.listen(process.env.PORT || 3000);
|
||||
})();
|
||||
|
|
|
@ -1,30 +1,32 @@
|
|||
'use strict';
|
||||
|
||||
const express = require('express');
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* @param {express.Request} req
|
||||
* @param {express.Response} res
|
||||
* @param {express.NextFunction} _next
|
||||
* @param {express.IRoute} routeObj
|
||||
*/
|
||||
get: async (req, res, _next) => {
|
||||
res.setHeader("content-type", "application/jrd+json");
|
||||
res.json(
|
||||
{
|
||||
"subject": `acct:${req.headers.host}@${req.headers.host}`,
|
||||
"aliases": [`https://${req.headers.host}/actor`],
|
||||
"links": [
|
||||
{
|
||||
"rel": "http://webfinger.net/rel/profile-page",
|
||||
"type": "text/html",
|
||||
"href": `https://${req.headers.host}/about/more?instance_actor=true`
|
||||
},
|
||||
{
|
||||
"rel": "self",
|
||||
"type": "application/activity+json",
|
||||
"href": `https://${req.headers.host}/actor`
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
route: (routeObj) => {
|
||||
routeObj.get(async (req, res, _next) => {
|
||||
res.setHeader("content-type", "application/jrd+json");
|
||||
res.json(
|
||||
{
|
||||
"subject": `acct:${req.headers.host}@${req.headers.host}`,
|
||||
"aliases": [`https://${req.headers.host}/actor`],
|
||||
"links": [
|
||||
{
|
||||
"rel": "http://webfinger.net/rel/profile-page",
|
||||
"type": "text/html",
|
||||
"href": `https://${req.headers.host}/about/more?instance_actor=true`
|
||||
},
|
||||
{
|
||||
"rel": "self",
|
||||
"type": "application/activity+json",
|
||||
"href": `https://${req.headers.host}/actor`
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,35 +5,35 @@ var jsonld = require('jsonld');
|
|||
|
||||
module.exports = {
|
||||
/**
|
||||
* @param {express.Request} req
|
||||
* @param {express.Response} res
|
||||
* @param {express.NextFunction} _next
|
||||
* @param {express.IRoute} routeObj
|
||||
*/
|
||||
post: async (req, res, _next) => {
|
||||
if ( req.body ) {
|
||||
var bodyParsed = await jsonld.compact(req.body, 'https://www.w3.org/ns/activitystreams');
|
||||
if ( bodyParsed.type === 'Create') {
|
||||
var to;
|
||||
var cc;
|
||||
if ( typeof bodyParsed.object.to === 'string' ) {
|
||||
to = [bodyParsed.object.to];
|
||||
} else {
|
||||
to = bodyParsed.object.to;
|
||||
route: (routeObj) => {
|
||||
routeObj.post(async (req, res, _next) => {
|
||||
if ( req.body ) {
|
||||
var bodyParsed = await jsonld.compact(req.body, 'https://www.w3.org/ns/activitystreams');
|
||||
if ( bodyParsed.type === 'Create') {
|
||||
var to;
|
||||
var cc;
|
||||
if ( typeof bodyParsed.object.to === 'string' ) {
|
||||
to = [bodyParsed.object.to];
|
||||
} else {
|
||||
to = bodyParsed.object.to;
|
||||
}
|
||||
if ( typeof bodyParsed.object.cc === 'string' ) {
|
||||
to = [bodyParsed.object.cc];
|
||||
} else {
|
||||
cc = bodyParsed.object.cc;
|
||||
}
|
||||
await db('inbox').insert({
|
||||
origin: bodyParsed.object.attributedTo,
|
||||
to: JSON.stringify(to),
|
||||
cc: JSON.stringify(cc),
|
||||
object: JSON.stringify(bodyParsed.object)
|
||||
});
|
||||
res.status(204);
|
||||
res.end();
|
||||
}
|
||||
if ( typeof bodyParsed.object.cc === 'string' ) {
|
||||
to = [bodyParsed.object.cc];
|
||||
} else {
|
||||
cc = bodyParsed.object.cc;
|
||||
}
|
||||
await db('inbox').insert({
|
||||
origin: bodyParsed.object.attributedTo,
|
||||
to: JSON.stringify(to),
|
||||
cc: JSON.stringify(cc),
|
||||
object: JSON.stringify(bodyParsed.object)
|
||||
});
|
||||
res.status(204);
|
||||
res.end();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue