Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. const express = require('express');
  2. const app = express();
  3. const debug = require('debug')('myapp:server');
  4. const path = require('path');
  5. const multer = require('multer');
  6. const logger = require('morgan');
  7. const serveIndex = require('serve-index')
  8.  
  9. var storage = multer.diskStorage({
  10. destination: (req, file, cb) => {
  11. cb(null, './public/uploads')
  12. },
  13. filename: (req, file, cb) => {
  14. cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
  15. }
  16. });
  17.  
  18. //will be using this for uplading
  19. const upload = multer({ storage: storage });
  20.  
  21. //get the router
  22. const userRouter =require('./routes/user.route');
  23.  
  24. app.use(logger('tiny'));
  25. app.use(express.json());
  26. app.use(express.urlencoded({ extended: false }));
  27. //app.use(express.static('public'));
  28. app.use('/ftp', express.static('public'), serveIndex('public', {'icons': true}));
  29.  
  30. app.get('/', function(req,res) {
  31. return res.send("hello from my app express server!")
  32. })
  33.  
  34. app.post('/testUpload', upload.single('file'), function(req,res) {
  35. debug(req.file);
  36. console.log('storage location is ', req.hostname +'/' + req.file.path);
  37. return res.send(req.file);
  38. })
  39.  
  40. //if end point is /users/, use the router.
  41. app.use('/users', userRouter);
  42.  
  43. const port = process.env.PORT || 3000;
  44. app.listen(port, () => {
  45. debug('Server is up and running on port ', port);
  46. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement