This repository has been archived by the owner on Sep 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (77 loc) · 2.19 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
var express = require('express');
var config = require('config');
var app = express();
var passport = require('passport')
var session = require('express-session')
var bodyParser = require('body-parser')
var path, crypto;
path = require('path');
crypto = require('crypto');
var multer = require('multer');
//Body-Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.set('trust proxy', 1) // trust first proxy
// For Passport
app.use(session({
secret: config.get("secret_key"),
resave: false,
saveUninitialized: true,
cookie: {
secure: true
}
}))
app.set("views", __dirname + "/apps/views");
app.set("view engine", "ejs"); // render dc data động cho client
var routes = require('./apps/routes/route');
routes(app);
//Static folder althernative for public
app.use("/static", express.static(__dirname + "/public"));
var controllers = require(__dirname + "/apps/controllers");
app.use(controllers);
var host = config.get("server.host");
var port = config.get("server.port");
var socket = require('socket.io');
var server = app.listen(port, host, function () {
console.log("Running", port);
});
let io = socket(server);
io.on('connection', function (socket) {
socket.on('client-send-log', function (msg) {
io.sockets.emit('message', msg);
console.log(msg);
});
});
var fs = require('fs');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/images/')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
});
var upload = multer({
storage: storage
});
// Post files
app.post('/upload', upload.single('upload'), function (req, res) {
console.log(req.file);
res.redirect("/uploads/" + req.file.filename);
return res.status(200).end();
});
app.get('/uploads/:upload', function (req, res) {
file = req.params.upload;
var img = fs.readFileSync(__dirname + "/public/images/" + file);
res.writeHead(200, {
'Content-Type': 'image/png'
});
res.end(img, 'binary');
});
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// next();
// });