Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const MongoClient = require('mongodb').MongoClient;
- const bodyParser = require('body-parser');
- const db = require('./config/db');
- const app = express();
- const port = 8000;
- app.use(bodyParser.urlencoded({ extended: true }));
- MongoClient.connect(db.url, (err, database) => {
- if (err) return console.log(err)
- require('./app/routes')(app, database);
- app.listen(port, () => {
- console.log('We are live on ' + port);
- })
- })
- module.exports = function(app, db) {
- app.post('/notes', (req, res) => {
- const note = { text: req.body.body, title: req.body.title };
- db.collection('notes').insert(note, (err, result) => {
- if (err) {
- res.send({ 'error': 'An error has occurred' });
- } else {
- res.send(result.ops[0]);
- }
- });
- });
- };
- mongoClient.connect("mongodb://myuser:pass@host:port/?ssl=true", {
- uri_decode_auth: true
- }, function (err, db) {
- db.close();
- });
- mongoClient.connect("mongodb://host:port/?ssl=true", {
- auth: {
- user: 'username',
- password: 'p@ssword',
- }}, function (err, db) {
- db.close();
- });
Add Comment
Please, Sign In to add comment