Last active
February 26, 2020 08:49
-
-
Save kamiazya/812666dad9e36967b0a079c685700182 to your computer and use it in GitHub Desktop.
nodeでmongoバックエンドのAPIサーバーを作る
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FROM node:9.11.1-alpine | |
| WORKDIR /app | |
| COPY package.json /app | |
| COPY package-lock.json /app | |
| RUN npm install --production | |
| EXPOSE 3000 | |
| COPY src /app/src | |
| CMD npm start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const NATS = require('nats'); | |
| var nats = NATS.connect({ | |
| servers: ['nats://nats:4222'] | |
| }); | |
| const app = express(); | |
| const assert = require('assert'); | |
| // MongoDB | |
| let db; | |
| const MongoClient = require('mongodb').MongoClient; | |
| const MONGO_URL = 'mongodb://mongo-0.mongo:27017,mongo-1.mongo:27017,mongo-2.mongo:27017/test?replicaSet=rs0'; | |
| // // Simple Publisher | |
| // nats.publish('foo', 'Hello World!'); | |
| const LISTS_COLLECTION_NAME = 'lists'; | |
| const mongoOptions = { | |
| replicaSet: 'rs0', | |
| ssl: false, | |
| sslValidate: false, | |
| poolSize: 5, | |
| reconnectTries: 1, | |
| }; | |
| MongoClient.connect(MONGO_URL, mongoOptions, function(err, mongodb) { | |
| assert.equal(null, err); | |
| console.log("Connected correctly to server"); | |
| db = mongodb; | |
| }); | |
| function collection(name) { | |
| return db.collection(name); | |
| } | |
| app.get('/', (req, res) => { | |
| // res.json({ | |
| // hello: 'world', | |
| // }); | |
| collection(LISTS_COLLECTION_NAME).find().toArray(function(err, docs){ | |
| res.send(docs); | |
| }) | |
| }); | |
| app.post('/', function (req, res, next) { | |
| collection(LISTS_COLLECTION_NAME).insertOne(req.body).then(function(r) { | |
| res.send(r); | |
| }); | |
| }); | |
| /* 2. listen()メソッドを実行して3000番ポートで待ち受け。*/ | |
| const server = app.listen(3000, function () { | |
| console.log("Node.js is listening to PORT:" + server.address().port); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "web-api", | |
| "version": "0.0.0", | |
| "description": "web-api", | |
| "main": "src/index.js", | |
| "scripts": { | |
| "start": "node src/index.js", | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "private": true, | |
| "license": "ISC", | |
| "dependencies": { | |
| "express": "^4.16.3", | |
| "mongodb": "^2.2.35", | |
| "nats": "^0.8.10" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment