-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (32 loc) · 986 Bytes
/
index.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
30
31
32
33
34
35
36
37
38
39
40
41
42
const uploadMiddleware = require('./middleware/uploadMiddleware');
const express = require('express');
require('dotenv').config();
const methodOverride = require('method-override');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
const upload = uploadMiddleware('images');
app.post('/upload', upload.single('image'), (req, res) => {
//promise
try{
if(!req.file) {
return res.status(400).json({message: 'Please upload a file'})
}
const imageUrl = req.file.path;
console.log(imageUrl)
}catch (error) {
console.log(error)
}
});
app.get('/', (req, res) => {
res.send(`
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<button type="submit">Upload</button>
</form>
`);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});