Created
February 19, 2016 22:38
-
-
Save mpaccione/cf6c9d2a10de2e354578 to your computer and use it in GitHub Desktop.
FileServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Read to end first, then solve one by one. | |
var express = require('express'); | |
// #0. Load 'multer' here. | |
var mul = require('multer'); | |
// #1. Use folder 'uploads' for multer's upload here. | |
var storage = mul.diskStorage({ | |
destination: function(req, file, cb){ | |
cb(null, './uploads') | |
}, | |
filename: function (req, file, cb) { | |
crypto.pseudoRandomBytes(16, function (err, raw) { | |
cb(null, raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype)); | |
}); | |
} | |
}); | |
var upload = mul({ storage: storage }); | |
var path = require('path'); | |
var app = express(); | |
// #6. Tune Express to serve static files from '/unloads' folder. | |
app.use(express.static('uploads')); | |
// #7. Choose a file in 'uploads', copy it's name, navigate to http://localhost:3000/uploads/chosen_file_name | |
// #5. Fix the line below to receive a sigle file from field named 'avatar' | |
// and look at the terminal. | |
app.post('/profile', upload.single('avatar'), function (req, res, next) { | |
// #2.a Add code here to return received file's info as JSON back to the client. | |
var json = JSON.stringify(req.body); | |
res.write(json); | |
// #2.b Log file's info to the console here. | |
console.log(json); | |
// #3.a Start the server in one terminal: npm start | |
// #3.b Run in another terminal to test posting data: | |
// curl -v --form "file=@test.txt" http://127.0.0.1:3000/profile | |
// #3.c Read messages in the console in the server's terminal. | |
// #4. Post a file with the form in index.html | |
}); | |
app.get('/', function(req, res) { | |
res.sendFile(path.join(__dirname + '/index.html')); | |
}); | |
app.get('/files', function(req, res){ | |
}); | |
app.post('/files', function(req, res){ | |
}); | |
app.delete('/files', function(req, res){ | |
}); | |
app.get('/files/:id', function(req, res){ | |
}); | |
app.use('/uploads', express.static('uploads')); | |
app.listen(3000, function () { | |
console.log('Your file app is listening on port 3000!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment