-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
223 lines (192 loc) · 6.33 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
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
// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const fs = require('fs');
const bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
const app = express();
var crypto = require("crypto");
var formidable = require('formidable');
var cors = require('cors');
var multer = require('multer');
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();
});
app.options('*', cors());
app.all('/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:4200");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
next();
});
app.get('/', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.writeHead(200, {'Content-Type': 'text/plain'});
});
// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Point static path to dist
app.use(express.static(path.join(__dirname, 'src')));
// Get our API routes
const api_forget = require('./routes/forgetPassword');
app.use('/api/nhatuyendung/', api_forget);
app.use('/uploads', express.static(__dirname + '/uploads'));
// Cross Origin middleware
//==================API TRANG WEB O DAY=========
// Set our api routes
var setRoutes =require('./routes/routes')
app.use('/api', setRoutes);
var Routes_forgetPassword =require('./routes/forgetPassword')
app.use('/api', Routes_forgetPassword);
app.post('/upload', function(req, res){
// create an incoming form object
var form = new formidable.IncomingForm();
// specify that we want to allow the user to upload multiple files in a single request
form.multiples = true;
// store all uploads in the /uploads directory
form.uploadDir = path.join(__dirname, '/uploads');
// every time a file has been uploaded successfully,
// rename it to it's orignal name
form.on('file', function(field, file) {
fs.rename(file.path, path.join(form.uploadDir, file.name));
});
// log any errors that occur
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
// once all the files have been uploaded, send a response to the client
form.on('end', function() {
res.end('success');
});
// parse the incoming request containing the form data
form.parse(req);
});
//=======Upload cach 2====
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./uploads/forms/post");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload = multer({
storage: Storage
}).single('file');
app.post("/api/uploadpostbaidang", function(req, res) {
upload(req, res, function(err) {
if (err) {
return res.end("Something went wrong!");
}
console.log(req.file.filename)
return res.end(req.file.filename);
});
});
//Upload logo
var Storage_logo = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./uploads/forms/logo");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload_logo = multer({
storage: Storage_logo
}).single('file_logo');
app.post("/api/uploadlogo", function(req, res) {
upload_logo (req, res, function(err) {
if (err) {
return res.end("Something went wrong!");
}
console.log(req.file.filename)
return res.end(req.file.filename);
});
});
//UploadANH DAI DIEN CONG TY
var Storage_cogty = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./uploads/forms/image");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload_cogty = multer({
storage: Storage_cogty
}).single('file_co');
app.post("/api/uploadcogty", function(req, res) {
upload_cogty (req, res, function(err) {
if (err) {
return res.end("Something went wrong!");
}
console.log(req.file.filename)
return res.end(req.file.filename);
});
});
//Upload anhdaidien
var Storage_anhdaidien = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./uploads/forms/anhdaidien");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload_anhdaidien = multer({
storage: Storage_anhdaidien
}).single('anhdaidien');
app.post("/api/anhdaidien", function(req, res) {
upload_logo (req, res, function(err) {
if (err) {
return res.end("Something went wrong!");
}
console.log(req.file.filename)
return res.end(req.file.filename);
});
});
//Upload cv
var Storage_cv = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./uploads/forms/filecv");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload_cv = multer({
storage: Storage_logo
}).single('file_cv');
app.post("/api/uploadcv", function(req, res) {
upload_cv (req, res, function(err) {
if (err) {
return res.end("Something went wrong!");
}
console.log(req.file.filename)
return res.end(req.file.filename);
});
});
//==================API TRANG WEB O DAY=========
// Catch all other routes and return the index file
app.get('/api/', (req, res) => {
res.sendFile(path.join(__dirname, 'src/index.html'));
});
//==================SET API =========
/**
* Get port from environment and store in Express.
*/
const port = process.env.PORT || '3000';
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port, () => console.log(`API running on localhost:${port}`));