-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodejs_server.js
29 lines (23 loc) · 1.04 KB
/
nodejs_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
// we use express and multer libraries to send images
const express = require('express');
const multer = require('multer');
const server = express();
const PORT = 3000;
// uploaded images are saved in the folder "/upload_images"
const upload = multer({dest: __dirname + '/upload_images'});
server.use(express.static('public'));
// "myfile" is the key of the http payload
server.post('/', upload.single('myfile'), function(request, respond) {
if(request.file) console.log(request.file);
// save the image
var fs = require('fs');
fs.rename(__dirname + '/upload_images/' + request.file.filename, __dirname + '/upload_images/' + request.file.originalname, function(err) {
if ( err ) console.log('ERROR: ' + err);
});
respond.end(request.file.originalname + ' uploaded!');
});
// You need to configure node.js to listen on 0.0.0.0 so it will be able to accept connections on all the IPs of your machine
const hostname = '0.0.0.0';
server.listen(PORT, hostname, () => {
console.log(`Server running at http://${hostname}:${PORT}/`);
});