-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (44 loc) · 1.39 KB
/
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
43
44
45
46
47
48
49
50
51
const express = require('express')
const bodyParser = require('body-parser')
const dotenv = require('dotenv')
const db = require('./config/dbconnection')
const userRoutes = require('./routes/user')
const postRoutes = require('./routes/post')
const commentRoutes = require('./routes/comment')
const multer = require('multer')
const cors = require('cors')
const path = require('path')
const { requireAuth } = require('./middleware/authMiddleware')
const app = express()
dotenv.config()
app.use(cors())
app.use('/images', express.static(path.join(__dirname, '/images')))
const distStorage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'images')
},
filename: (req, file, cb) => {
cb(null, req.body.name)
},
})
const upload = multer({ storage: distStorage })
app.post('/api/upload', upload.single('image'), (req, res) => {
res.status(200).json({
message: 'file has been uploaded',
imgName: req.file.filename,
})
})
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use('/api/user', userRoutes)
app.use('/api/post', postRoutes)
app.use('/api/comment', commentRoutes)
app.get('/', (req, res) => {
res.send('Welcome into bookshelf project')
})
app.get('/api/users', (req, res) => {
res.send('Welcome into bookshelf project')
})
app.listen(process.env.PORT, function () {
console.log('Server is listening at port:' + process.env.PORT)
})