Guest User

Untitled

a guest
Jul 23rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. const express = require('express');
  2. const MongoClient = require('mongodb').MongoClient;
  3. const bodyParser = require('body-parser');
  4. const db = require('./config/db');
  5. const app = express();
  6. const port = 8000;
  7. app.use(bodyParser.urlencoded({ extended: true }));
  8. MongoClient.connect(db.url, (err, database) => {
  9. if (err) return console.log(err)
  10. require('./app/routes')(app, database);
  11. app.listen(port, () => {
  12. console.log('We are live on ' + port);
  13. })
  14. })
  15.  
  16. module.exports = function(app, db) {
  17. app.post('/notes', (req, res) => {
  18. const note = { text: req.body.body, title: req.body.title };
  19. db.collection('notes').insert(note, (err, result) => {
  20. if (err) {
  21. res.send({ 'error': 'An error has occurred' });
  22. } else {
  23. res.send(result.ops[0]);
  24. }
  25. });
  26. });
  27. };
  28.  
  29. mongoClient.connect("mongodb://myuser:pass@host:port/?ssl=true", {
  30. uri_decode_auth: true
  31. }, function (err, db) {
  32. db.close();
  33. });
  34.  
  35. mongoClient.connect("mongodb://host:port/?ssl=true", {
  36. auth: {
  37. user: 'username',
  38. password: 'p@ssword',
  39. }}, function (err, db) {
  40. db.close();
  41. });
Add Comment
Please, Sign In to add comment