Last active
February 18, 2019 10:19
-
-
Save Bilguun132/4ca4a6982195e8d21dc978a774d64688 to your computer and use it in GitHub Desktop.
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
const express = require('express'); | |
const app = express(); | |
const debug = require('debug')('myapp:server'); | |
const path = require('path'); | |
const multer = require('multer'); | |
const logger = require('morgan'); | |
const serveIndex = require('serve-index') | |
var storage = multer.diskStorage({ | |
destination: (req, file, cb) => { | |
cb(null, './public/uploads') | |
}, | |
filename: (req, file, cb) => { | |
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname)) | |
} | |
}); | |
//will be using this for uplading | |
const upload = multer({ storage: storage }); | |
//get the router | |
const userRouter =require('./routes/user.route'); | |
app.use(logger('tiny')); | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: false })); | |
//app.use(express.static('public')); | |
app.use('/ftp', express.static('public'), serveIndex('public', {'icons': true})); | |
app.get('/', function(req,res) { | |
return res.send("hello from my app express server!") | |
}) | |
app.post('/testUpload', upload.single('file'), function(req,res) { | |
debug(req.file); | |
console.log('storage location is ', req.hostname +'/' + req.file.path); | |
return res.send(req.file); | |
}) | |
//if end point is /users/, use the router. | |
app.use('/users', userRouter); | |
const port = process.env.PORT || 3000; | |
app.listen(port, () => { | |
debug('Server is up and running on port ', port); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment