This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
express_webserver.js
53 lines (39 loc) · 1.76 KB
/
express_webserver.js
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
53
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var querystring = require('querystring');
var debug = require('debug')('botkit:webserver');
var http = require('http');
var hbs = require('express-hbs');
module.exports = function(controller) {
var webserver = express();
webserver.use(cookieParser());
webserver.use(bodyParser.json());
webserver.use(bodyParser.urlencoded({ extended: true }));
// set up handlebars ready for tabs
webserver.engine('hbs', hbs.express4({partialsDir: __dirname + '/../views/partials'}));
webserver.set('view engine', 'hbs');
webserver.set('views', __dirname + '/../views/');
// import express middlewares that are present in /components/express_middleware
var normalizedPath = require("path").join(__dirname, "express_middleware");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./express_middleware/" + file)(webserver, controller);
});
webserver.use(express.static('public'));
webserver.use(function(req, res, next) {
console.log('REQ > ', req.url);
next();
});
var server = http.createServer(webserver);
server.listen(process.env.PORT || 3000, null, function() {
console.log('Express webserver configured and listening at http://localhost:' + process.env.PORT || 3000);
});
// import all the pre-defined routes that are present in /components/routes
var normalizedPath = require("path").join(__dirname, "routes");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./routes/" + file)(webserver, controller);
});
controller.webserver = webserver;
controller.httpserver = server;
return webserver;
}