-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (113 loc) · 3.39 KB
/
app.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var express = require('express'),
app = express(),
http = require('http').createServer(app),
fs = require('fs'),
path = require('path'),
i18n = require('i18n'),
moment = require('moment'),
io = require('socket.io')(http)
mime = require('mime'),
favicon = require('serve-favicon'),
request = require('request'),
builder = require('./builder'),
ECT = require('ect'),
ectRenderer = ECT({ watch: true, root: __dirname + '/client', ext: '.ect'}),
helpers = {},
helpersPath = './server/helpers/';
// App configure
app
.set('isDev', app.get('env') == 'development')
.set('config', require('./configs/config'))
.set('port', process.env.PORT || 3000)
.set('views', path.join(__dirname, 'client'))
.set('view engine', 'ect')
.engine('ect', ectRenderer.render)
.use(favicon(__dirname + '/favicon.ico'))
/*.use(express.static(path.join(__dirname, 'client')))*/;
// i18n configure
i18n.configure({
locales: app.get('config').locales,
updateFiles: false,
directory: './locales'
});
// Helpers
fs.readdirSync(helpersPath).forEach(function(file) {
var _file = file.split('.'),
filePath = helpersPath + file;
helpers[_file[0]] = require(filePath);
});
// Rewrite res.render function
app.all('*', function (req, res, next) {
var render = res.render;
res.render = function (view, data) {
res.render = render;
var langs = req.get('accept-language') || '',
lang = langs.replace(/(-|,)(.*)/, ''),
view;
if (app.get('config').locales.indexOf(lang) != -1) {
i18n.setLocale(lang);
moment.locale(lang);
}
app.locals.Config = app.get('config');
app.locals.Request = req;
app.locals.Lang = lang;
app.locals.Helpers = helpers;
app.locals.Data = {
footerNav: require('./data/footerNav'),
categories: require('./data/categories')
};
res.render('layouts/main/main', {view: view, data: data});
}
next();
});
app.get(/\.(styl|scss|less)$/, function (req, res, next) {
var pathname = req._parsedUrl.pathname;
builder.renderFile(pathname, function (data) {
res.type('css');
res.end(data);
});
});
app.get(/\.(js|css|png|jpg|gif|svg|eot|ttf|woff|woff2)$/, function (req, res, next) {
var pathname = req._parsedUrl.pathname,
filePath = process.cwd() + '/client' + pathname;
res.type(mime.lookup(filePath));
res.end(fs.readFileSync(filePath));
});
app.get(/\.(json)$/, function (req, res, next) {
var pathname = req._parsedUrl.pathname,
filePath = process.cwd() + pathname;
res.type(mime.lookup(filePath));
res.end(fs.readFileSync(filePath));
});
app.get('/json/external', function (req, res, next) {
request.get(req.query.url, req.query.params, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response)
res.json({});
}
});
});
app.get('/', function (req, res, next) {
res.render('index', {
reviews: require('./data/reviews')
});
});
app.get('/category', function (req, res, next) {
res.render('category', {
category: require('./data/category')
});
});
app.get('/company', function (req, res, next) {
res.render('company', {
company: require('./data/company'),
category: require('./data/category')
});
});
// Socet.IO
io.on('connection', function (socket) {
console.log('a user connected');
});
// Start server
http.listen(process.env.PORT || 3000, function () {
console.log('Server started');
});