-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilsHttp.js
More file actions
53 lines (46 loc) · 1.28 KB
/
Copy pathutilsHttp.js
File metadata and controls
53 lines (46 loc) · 1.28 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var queryStr = require('querystring')
var fs = require('fs')
var ejs = require('ejs')
var path = require('path')
var utilsHttp = this;
var Chuck = require('./chuck')
utilsHttp.defaultViews = path.join(__dirname, "views")
utilsHttp.renderHtml = function(view, res, options, viewsDir) {
_this = this
viewsDir = viewsDir || utilsHttp.defaultViews;
var filepath = path.join(viewsDir, view);
fs.readFile(filepath, 'utf8', function(error, view) {
if(error) this.error404(res);
else
var html = ejs.render(view, options);
res.writeHead(200, {'Content-type': 'text/html'})
res.end(html);
});
}
utilsHttp.parser = function(req, cBack) {
var body = "";
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
cBack(queryStr.parse(body));
});
}
// utilsHttp.addCSS = function(req, res) {
// fs.readFile('public/css/style.css', function(error, data) {
// if (error) console.log(error);
// res.writeHead(200, {'Content-type': 'text/css'});
// res.end(data);
// });
// }
utilsHttp.error404 = function(res) {
res.writeHead(404);
res.end('Whoops...404 - page not found');
}
utilsHttp.redirect = function(url, response) {
response.writeHead(302, {
'Content-Type': 'text/html',
'Location': url
});
response.end();
}