Provides a Micropub route for Express 4.x
Node.js requirement is set in package.json
(Use eg. installed-check to validate that it complies with your requirements)
npm install micropub-express --save
Early alpha
Supported:
- Creation of content based items and creation of likes
The rest of the CRUD-operations + other more complex operations are yet to be built and the API might change to adopt to the requirements of those. Versioning will stick to Semantic Versioning to clearly communicate such breaking changes.
var micropub = require('micropub-express');
// Attach the micropub endpoint to "/micropub" or wherever else you want
app.use('/micropub', micropub({
// Specify what endpoint you want to verify a token with and what the expected identity returned is
tokenReference: {
me: 'http://example.com/',
endpoint: 'https://tokens.indieauth.com/token',
},
// And lastly: Do something with the created micropub document
handler: function (micropubDocument, req) {
// Do something with the micropubDocument and return a Promise to communicate status of the handling
return Promise.resolve().then(function () {
return { url: 'http://example.com/url/to/new/post' };
});
}
}));
var express = require('express');
var micropub = require('micropub-express');
var app = express();
// Do some Express magic to support multiple Micropub endpoints in the same application
app.param('targetsite', function (req, res, next, id) {
// Resolve a token reference from the "targetsite" id and return 404 if you find no match
if (id === 'example.com') {
req.targetsite = {
me: 'http://example.com/',
endpoint: 'https://tokens.indieauth.com/token',
};
next();
} else {
res.sendStatus(404);
}
});
app.use('/micropub/:targetsite', micropub({
logger: logger, // a logger object that uses the same API as the bunyan module
userAgent: 'my-app/1.0', // a user-agent that will be prepended to the module's own user-agent to indicate
// to IndieAuth endpoints who it is that makes the verification requests
tokenReference: function (req) {
// Find the token reference we added to the request object before and return it
return req.targetsite;
},
// And lastly: Do something with the created micropub document
handler: function (micropubDocument, req) {
// Do something with the micropubDocument and return a Promise to communicate status of the handling
return Promise.resolve().then(function () {
return { url: 'http://example.com/url/to/new/post' };
});
}
}));
// Start the Express server on a port, like port 3000!
app.listen(3000);
- tokenReference – required – either an object with two keys,
me
andendpoint
, or a function that receives the request object and returns an object with those two keys. Theme
key signify what identity it is that's expected for a succesful authorization and theendpoint
key indicates what endpoint the token should be verified with. Can also be or return an array of multiple references. - handler – required – the function that will be called with the handled micropub document and the request object. It's this functions responsibility to actually act on the received data and do something with it. Should return a
Promise
resolving to an object with aurl
key containing the url of the created item to indicate success. If thePromise
is rejected or theurl
key is missing or falsy in the resolvedPromise
, then a400
error will be returned to indicate failure. - userAgent – recommended – a user-agent string like
your-app-name/1.2.3 (http://app.example.com/)
that gets prepended to the user-agent ofmicropub-express
itself when verifying received tokens against an endpoint - queryHandler – optional – a function that will be called whenever a
?q=
query is made to the Micropub endpoint. It's this functions responsibility to execute the query and respond with the relevant data. Should return aPromise
resolving to an object containing the query result. Keys on the object should not include any[]
, those will be added in the encoded result where relevant. If thePromise
resolves to something falsy, then a400
error will be returned to indicate that the query type is unsupported. If thePromise
is rejected, then a400
error will be returned to indicate failure. - logger – optional – a bunyan compatible logger, like bunyan itself or some other module. Defaults to bunyan-duckling which logs with
console.log()
andconsole.error()
The format closely matches the JSON-representation of Micropub.
It contains three top level keys:
- type – an array containing the type that is that's going to be created. Eg.
['h-entry']
- properties – an object containing all of the microformat properties of the document as arrays containing strings. Eg.
content: ['foobar']
- mp – an object containing all of the micropub directives as arrays containing string. Eg.
'syndicate-to': ['http://twitter.com/example']
for an'mp-syndicate-to'
directive. - files – an object that can contain three keys,
audio
,video
,photo
, which in turn contains arrays of objects with afilename
and abuffer
key with the name and content of the files.
Full example:
{
type: ['h-entry'],
properties: {
content: ['hello world'],
},
mp: {
'syndicate-to': ['http://twitter.com/example'],
},
files: {
photo: [
{
filename: 'example.jpg',
buffer: new Buffer() // A Node.js buffer with the content of the file.
}
]
}
}
- format-microformat – a module that takes a
micropubDocument
as its input and then formats filenames, URL:s and file content from that data so one gets some standard data which one then can publish elsewhere – like to a Jekyll blog or something. - github-publish – a module that takes a filename and content and publishes that to a GitHub repository. A useful place to send the formatted data that comes out of
format-microformat
if one wants to add it to a GitHub hosted Jekyll blog of some kind, like eg. GitHub Pages.