-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
41 lines (35 loc) · 897 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
const express = require('express');
const app = express();
require('dotenv').config();
app.set('json spaces', 5); // to pretify json response
const PORT = process.env.PORT;
const fileparser = require('./fileparser');
app.get('/', (req, res) => {
res.send(`
<h2>File Upload With <code>"Node.js"</code></h2>
<form action="/api/upload" enctype="multipart/form-data" method="post">
<div>Select a file:
<input name="file" type="file" />
</div>
<input type="submit" value="Upload" />
</form>
`);
});
app.post('/api/upload', async (req, res) => {
await fileparser(req)
.then(data => {
res.status(200).json({
message: "Success",
data
})
})
.catch(error => {
res.status(400).json({
message: "An error occurred.",
error
})
})
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}.`);
})