Created
July 13, 2022 10:55
-
-
Save kyungjoongo/0b11e2736a96c1102d5e37ed1287ff32 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 handlebars = require('express-handlebars'); | |
const bodyParser = require("body-parser"); | |
const cors = require("cors"); | |
const path = require('path'); | |
const app = express(); | |
app.engine('.hbs', handlebars({ | |
layoutsDir: __dirname + '/views/layouts', | |
defaultLayout: null, | |
extname: '.hbs' | |
})); | |
app.use(express.static(__dirname + '/public')); | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', '.hbs'); | |
app.use(cors()); | |
app.use(bodyParser.urlencoded({extended: true})); | |
const multer = require('multer'); | |
app.use(bodyParser.json()); | |
const storage = multer.diskStorage({ | |
destination(req, file, callback) { | |
callback(null, './images'); | |
}, | |
filename(req, file, callback) { | |
callback(null, `${file.fieldname}_${Date.now()}_${file.originalname}`); | |
}, | |
}); | |
const upload = multer({ storage }); | |
app.get('/', (req, res) => { | |
res.status(200).send('You can post to /api/upload.'); | |
}); | |
app.post('/api/upload', upload.any(), (req, res) => { | |
console.log('file', req.files); | |
console.log('body', req.body); | |
res.status(200).json({ | |
message: 'success!', | |
}); | |
}); | |
app.listen(process.env.PORT || 3000, () => { | |
console.log( | |
`server is running at http://localhost:${process.env.PORT || 3000}` | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment