-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
125 lines (96 loc) · 3.28 KB
/
server.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
const express = require('express');
//const app = express();
//const bodyParser = require('body-parser');
const database = require('./config/database');
const passport = require('passport');
const User = require('./models/user');
const cors = require('cors');
let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
//const formidable = require('express-formidable');
var formidable = require('formidable');
var path = require('path');
var multer = require('multer');
var upload = multer({ dest: 'uploads/'});
var fs = require('fs');
const port = process.env.PORT || 8080;
const users = require('./routes/users');
const messages = require('./routes/messages');
app.use(express.static(path.join(__dirname + '/public')));
// headers and content type
/*
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT');
res.setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');
next();
});
*/
var type = upload.single('photo');
app.post('/upload', type, function (req,res) {
console.log(req);
/** When using the "single"
data come in "req.file" regardless of the attribute "name". **/
var tmp_path = req.file.path;
/** The original name of the uploaded file
stored in the variable "originalname". **/
var target_path = 'uploads/' + req.file.originalname;
/** A better way to copy the uploaded file. **/
var src = fs.createReadStream(tmp_path);
var dest = fs.createWriteStream(target_path);
src.pipe(dest);
//fs.unlink(tmp_path); //deleting the tmp_path
src.on('end', function() { return res.json({msg:'It worked!!'}); });
src.on('error', function(err) { return res.json({msg:'It failed...!!'}); });
});
// formidable
app.use(function (req, res, next) {
var form = new formidable.IncomingForm({
encoding: 'utf-8',
uploadDir: './tmp/images',
multiples: true,
keepExtensions: true
});
form.once('error', console.log);
form.parse(req, function (err, fields, files) {
Object.assign(req, {fields, files});
next();
});
});
//Middleware BodyParser
//app.use(bodyParser.json());
app.use(cors());
//Middleware Passport Auth
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
app.use('/users', users);
app.use('/messages', messages);
app.get('/', (req, res) => {
res.send('It works');
res.end();
});
io.on('connection', (socket) => {
// Log whenever a user connects
console.log('user connected');
// Log whenever a client disconnects from our websocket server
socket.on('disconnect', function(){
console.log('user disconnected');
});
// When we receive a 'message' event from our client, print out
// the contents of that message and then echo it back to our client
// using `io.emit()`
socket.on('message', (data) => {
console.log('Message Received: ' + data);
io.emit('message', data);
});
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
http.listen(port, ( err ) => {
if(err) throw err;
console.log('Connected on port ' + port);
});