|
| 1 | +const bodyParser = require('body-parser'); |
| 2 | +const express = require('express'); |
| 3 | +const mongoose = require('mongoose'); |
| 4 | + |
| 5 | +const notesApi = require('./notes-api'); |
| 6 | +const usersApi = require('./users-api'); |
| 7 | + |
| 8 | +const addSampleData = module.exports.addSampleData = async () => { |
| 9 | + const [userA, userB] = await User.create([ |
| 10 | + { |
| 11 | + name: "A", |
| 12 | + token: "tokenA" |
| 13 | + }, |
| 14 | + { |
| 15 | + name: "B", |
| 16 | + token: "tokenB" |
| 17 | + } |
| 18 | + ]); |
| 19 | + |
| 20 | + await Note.create([ |
| 21 | + { |
| 22 | + title: "Public note belonging to A", |
| 23 | + body: "This is a public note belonging to A", |
| 24 | + isPublic: true, |
| 25 | + ownerToken: userA.token |
| 26 | + }, |
| 27 | + { |
| 28 | + title: "Public note belonging to B", |
| 29 | + body: "This is a public note belonging to B", |
| 30 | + isPublic: true, |
| 31 | + ownerToken: userB.token |
| 32 | + }, |
| 33 | + { |
| 34 | + title: "Private note belonging to A", |
| 35 | + body: "This is a private note belonging to A", |
| 36 | + ownerToken: userA.token |
| 37 | + }, |
| 38 | + { |
| 39 | + title: "Private note belonging to B", |
| 40 | + body: "This is a private note belonging to B", |
| 41 | + ownerToken: userB.token |
| 42 | + } |
| 43 | + ]); |
| 44 | +} |
| 45 | + |
| 46 | +module.exports.startApp = async () => { |
| 47 | + // Open the default mongoose connection |
| 48 | + await mongoose.connect('mongodb://mongo:27017/notes', { useFindAndModify: false }); |
| 49 | + // Drop contents of DB |
| 50 | + mongoose.connection.dropDatabase(); |
| 51 | + // Add some sample data |
| 52 | + await addSampleData(); |
| 53 | + |
| 54 | + const app = express(); |
| 55 | + |
| 56 | + app.use(bodyParser.json()); |
| 57 | + app.use(bodyParser.urlencoded()); |
| 58 | + |
| 59 | + app.get('/', async (_req, res) => { |
| 60 | + res.send('Hello World'); |
| 61 | + }); |
| 62 | + |
| 63 | + app.use('/api/notes', notesApi.router); |
| 64 | + app.use('/api/users', usersApi.router); |
| 65 | + |
| 66 | + app.listen(3000); |
| 67 | + Logger.log('Express started on port 3000'); |
| 68 | +}; |
0 commit comments