-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddlewares.js
More file actions
27 lines (22 loc) · 783 Bytes
/
middlewares.js
File metadata and controls
27 lines (22 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var fs = require('fs');
var parseUrl = require('url').parse;
module.exports = {
pub: function(req, res, next) {
var url = parseUrl(req.url);
if (!url.pathname.match(/public\//)) return next();
fs.readFile('.' + url.pathname, function(err, buffer) {
res.end(buffer.toString());
});
},
json: function(req, res, next) {
if (req.method != 'POST') return next();
var response = { posted_data: req.body };
res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
res.end(JSON.stringify(response));
},
any: function (req, res) {
var url = parseUrl(req.url);
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
res.end('Opa! Esse é o conteúdo da <strong>' + url.pathname + '</strong>');
}
}