This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
422 lines (347 loc) · 11.6 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
var functions = require('./libs/functions.js');
var express = require('express'),
config = require('./config/config'),
glob = require('glob'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
jade = require('jade'),
path = require('path'),
multipart = require('connect-multiparty'),
fs = require('fs'),
multipartMiddleware = multipart();
// mongoose.connect(config.db);
mongoose.connect('mongodb://techkids:[email protected]:21751/techkids');
var dbMongo = mongoose.connection;
dbMongo.on('error', function () {
throw new Error('unable to connect to database at ' + config.db);
});
// ------------dữ liệu-----------------------
// var models = glob.sync(config.root + '/app/models/*.js');
// models.forEach(function (model) {
// require(model);
// });
var PostSchema = mongoose.Schema({
title : String,
slug : String,
picture : String,
teaser : String,
content : String,
author: String,
time : Number
});
// ---- kiểm tra tình trạng kết nối mongo -----
var Post = mongoose.model('Post', PostSchema);
dbMongo.on('error', console.error.bind(console, 'connection error:'));
dbMongo.once('open', function(){
console.log('MongoDb connected');
});
//=========================================================
//app init
var app = express();
// the directory where the template files are located.
// var viewPath = path.join(__dirname, 'app/views/');
// var viewPath ='/views'
// app.set('views', viewPath);
// the template engine to use.
app.set('view engine', 'jade');
app.engine('jade', require('jade').__express);
//upload folder
app.use(express.static(path.join(__dirname, 'public')));
app.use('/pictures/', express.static(__dirname + '/public/upload/'));
//=========================
var router = express.Router();
router.use(function(req, res, next) {
console.log(req.method, req.url);
next();
});
app.use('/', router);
//========================================
//<------------------static---------------------------------------------------->
// Đường dẫn tới thư mục pulic
//app.use('/cms/', express.static(__dirname + '/public/'));
app.use('/cms',express.static(path.join(__dirname, 'public')));
// =================demo hệ thống mới==============
app.use(bodyParser.urlencoded({
extended: true
}));
// Handle request
app.get('/test', function(req, res){
var posts = Post.find({}, function(err, result) {
// Sort by blog latest khó hiểu
result = result.sort({'id' : -1});
res.render('test', { title : 'Home page' , posts : result, functions : functions});
});
});
app.get('/post/:title/:id.html', function(req, res) {
var id = req.params.id || 0;
Post.findById(id, function(err, post) {
if(post) {
res.render('post/detail', {title : post.title, post : post});
return false;
}
res.render('error');
});
});
app.get('/create-post', function(req, res) {
res.render('post/create', { title : 'Create a post' });
});
app.post('/create-post', multipartMiddleware, function(req, res) {
var post = new Post;
post.title = req.body.title;
post.slug = functions.removeAccent(req.body.title);
post.teaser = req.body.teaser;
post.content = req.body.content;
var file = req.files.picture;
var originalFilename = file.name;
var fileType = file.type.split('/')[1];
var fileSize = file.size;
var pathUpload = __dirname + '/public/upload/' + originalFilename;
var data = fs.readFileSync(file.path);
fs.writeFileSync(pathUpload, data);
if( fs.existsSync(pathUpload) ) {
post.picture = originalFilename;
}
post.save(function(err, obj) {
if(!err) {
res.render('post/create', { status : 'success', message : 'Post successful!' });
return false;
}
});
});
//----------------------------route ------------------------------------------//
app.get('/summer-code-camp', function(req,res) {
res.sendFile(__dirname + '/public/Summer Camp/index.html');
});
app.get('/portfolio/web', function(req,res) {
res.sendFile(__dirname + '/public/Web/index.html');
});
app.get('/portfolio/mobile', function(req,res) {
res.sendFile(__dirname + '/public/Mobile/index.html');
});
app.get('/connect', function(req,res) {
res.sendFile(__dirname + '/public/Techkids Connect/index.html');
});
app.get('/chia-se/tai-lieu/chia-se-sach-cho-dan-lap-trinh-vien', function (req, res) {
res.render('articles-1');
});
app.get('/chuong-trinh/su-kien/english-and-it-why-how-what', function (req, res) {
res.render('events-1');
});
app.get('/chuong-trinh/su-kien/de-co-01-nam-kinh-nghiem-sinh-vien-it-nen-lam-the-nao', function (req, res) {
res.render('events-2');
});
app.get('/chuong-trinh/su-kien/tim-kiem-nhung-dua-tre-cong-nghe-2016', function (req, res) {
res.render('events-3');
});
app.get('/chuong-trinh/su-kien/hoc-lap-trinh-mien-phi-1-thang', function (req, res) {
res.render('events-4');
});
app.get('/chuong-trinh/su-kien/coding-work-shop-2', function (req, res) {
res.render('events-5');
});
app.get('/chuong-trinh/su-kien/free-training-google-apps', function (req, res) {
res.render('events-6');
});
app.get('/chuong-trinh/su-kien/keu-goi-500-ae', function (req, res) {
res.render('events-7');
});
app.get('/chuong-trinh/su-kien/coding-workshop-learn-to-code-should-i', function (req, res) {
res.render('events-8');
});
app.get('/chuong-trinh/su-kien/coding-for-women', function (req, res) {
res.render('events-9');
});
app.get('/chuong-trinh/su-kien/workshop-c4e', function (req, res) {
res.render('events-10');
});
// create a route to render the index.jade file.
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!'});
});
app.get('/shit', function (req, res) {
res.render('index2');
});
// create a route to render the about-us.jade file.
app.get('/about-us', function (req, res) {
res.render('about-us');
});
app.get('/ve-chung-toi', function (req, res) {
res.render('about-us');
});
// create a route to render the courses.jade file.
app.get('/courses', function (req, res) {
res.render('courses');
});
app.get('/khoa-hoc', function (req, res) {
res.render('courses');
});
// create a route to render the contact.jade file.
app.get('/contact', function (req, res) {
res.render('contact');
});
app.get('/lien-he', function (req, res) {
res.render('contact');
});
// create a route to render the event.jade file.
app.get('/events', function (req, res) {
res.render('events');
});
app.get('/chuong-trinh', function (req, res) {
res.render('events');
});
// create a route to render the articles.jade file.
app.get('/articles', function (req, res) {
res.render('articles');
});
app.get('/chia-se/', function (req, res) {
res.render('articles');
});
// create a route to render the portfolio.jade file.
// app.get('/portfolio', function (req, res) {
// res.render('portfolio');
// });
// app.get('/portfolio/app', function (req, res) {
// res.render('portfolio_app');
// });
// we have to create router here
// CREATE ROUTER
//==================================
// get an instance of router
var router_instructors = express.Router();
// create a route to render the intructors-1.jade file.
router_instructors.get('/1', function (req, res) {
res.render('instructors-1');
});
router_instructors.get('/2', function (req, res) {
res.render('instructors-2');
});
router_instructors.get('/3', function (req, res) {
res.render('instructors-3');
});
router_instructors.get('/4', function (req, res) {
res.render('instructors-4');
});
router_instructors.get('/le-hoang-tu', function (req, res) {
res.render('instructors-1');
});
router_instructors.get('/bui-xuan-canh', function (req, res) {
res.render('instructors-2');
});
router_instructors.get('/do-anh-tu', function (req, res) {
res.render('instructors-3');
});
router_instructors.get('/nguyen-tien-dat', function (req, res) {
res.render('instructors-4');
});
router_instructors.get('/nguyen-si-thanh-son', function (req, res) {
res.render('instructors-5');
});
router_instructors.get('/ton-hong-duc', function (req, res) {
res.render('instructors-6');
});
router_instructors.get('/tran-quang-hiep', function (req, res) {
res.render('instructors-7');
});
router_instructors.get('/cuong-nguyen', function (req, res) {
res.render('instructors-8');
});
router_instructors.get('/le-tien-dung', function (req, res) {
res.render('instructors-9');
});
router_instructors.get('/ta-hoang-minh', function (req, res) {
res.render('instructors-10');
});
router_instructors.get('/nguyen-quang-huy', function (req, res) {
res.render('instructors-11');
});
router_instructors.get('/nguyen-quang-huy', function (req, res) {
res.render('instructors-11');
});
// Đường dẫn tới thư mục pulic, static file cho instructor
router_instructors.use('/cms/', express.static(__dirname + '/public/'));
app.use('/instructors',router_instructors)
app.use('/giang-vien',router_instructors)
//router cho courses
//-------------------
// get an instance of router
var router_courses = express.Router();
// create a route to render the intructors-1.jade file.
router_courses.get('/1', function (req, res) {
res.render('courses-1');
});
router_courses.get('/2', function (req, res) {
res.render('courses-2');
});
router_courses.get('/3', function (req, res) {
res.render('courses-3');
});
router_courses.get('/4', function (req, res) {
res.render('courses-4');
});
router_courses.get('/code-for-everyone', function (req, res) {
res.render('courses-1');
});
router_courses.get('/iOS', function (req, res) {
res.render('courses-2');
});
router_courses.get('/android', function (req, res) {
res.render('courses-3');
});
router_courses.get('/web-fullstack', function (req, res) {
res.render('courses-4');
});
router_courses.get('/code-for-kids', function (req, res) {
res.render('courses-5');
});
router_courses.get('/test', function (req, res) {
res.render('test');
});
// Đường dẫn tới thư mục pulic, static file cho instructor
router_courses.use('/cms/', express.static(__dirname + '/public/'));
app.use('/courses',router_courses)
app.use('/khoa-hoc',router_courses)
//===================================
//router cho articles
//-------------------
// get an instance of router
var router_articles = express.Router();
// create a route to render the intructors-1.jade file.
router_articles.get('/chia-se-sach-cho-dan-lap-trinh-vien', function (req, res) {
res.render('articles-1');
});
// Đường dẫn tới thư mục pulic, static file cho instructor
router_articles.use('/cms/', express.static(__dirname + '/public/'));
app.use('/chia-se/',router_articles)
//===================================
//===================================
//router cho events
//-------------------
// get an instance of router
var router_events = express.Router();
// create a route to render the intructors-1.jade file.
router_events.get('/english-and-it-why-how-what', function (req, res) {
res.render('events-1');
});
// Đường dẫn tới thư mục pulic, static file cho instructor
router_events.use('/cms/', express.static(__dirname + '/public/'));
app.use('/chuong-trinh/',router_events)
//===================================
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get((req, res,err) => {
res.render('error'); // render jade file: views/error
console.log({err,data});
});
app.route('/*')
.get((req, res, err) => {
res.render('error');
console.log({err,data});
});
//</ FIXME:--------------------------route ----------------------------------------->//
// TODO:app.get('/', function (req, res) {
// res.send('Hello World!');
// });
console.log('so sad')
require('./config/express')(app, config);
app.listen(config.port, function () {
console.log('Express server listening on port ' + config.port);
});