Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
an action to send files
// ...
config.servers.web.rootEndpointType = 'api'
// ...
var fs = require('fs');
exports.action = {
name: 'file',
description: 'I send files',
matchExtensionMimeType: true,
version: 1.0,
toDocument: true,
inputs: {
path: {required: false},
},
render404: function(api, connection, next){
connection.rawConnection.responseHttpCode = 404;
connection.sendFile('404.html');
next(connection, false);
},
renderFile: function(api, connection, next){
// do any pre-rendering etc here
connection.rawConnection.responseHttpCode = 200;
connection.sendFile(connection.params.path);
next(connection, false);
},
run: function(api, connection, next){
var self = this;
if(connection.params.path == null){
self.render404(api, connection, next);
}else{
var file = api.config.general.paths.public + '/' + connection.params.path
console.log(file);
fs.exists(file, function(found){
if(found){
self.renderFile(api, connection, next);
}else{
self.render404(api, connection, next);
}
});
}
}
};
exports.routes = {
all: [
{ path: '/:path', action: 'file' },
{ path: '/', action: 'file' }
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment