-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
29 lines (23 loc) · 921 Bytes
/
server.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
import express from 'express'
import bodyParser from 'body-parser'
import mongoose from 'mongoose'
import Character from './character'
const app = express()
const port = 3001
const dbUrl = 'mongodb://localhost/deresute'
app.use(bodyParser.urlencoded({ extended:true }))
app.use(bodyParser.json())
mongoose.connect(dbUrl,dbErr => {
if(dbErr) throw new Error(dbErr)
else console.log('db connect')
app.get('/api/characters', (request, response) => {
Character.find({},(err, characterArray) => { // 取得したドキュメントをクライアント側と同じくcharacterArrayと命名
if (err) response.status(500).send()
else response.status(200).send(characterArray) // characterArrayをレスポンスとして送り返す
}).sort(({'charaid':1}))
})
app.listen(port,err => {
if(err) throw new Error(err)
else console.log(`port ${port}`)
})
})