SHARE
TWEET

Untitled

a guest Apr 25th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //require dependencies
  2. var express = require('express');
  3. var mongoose = require('mongoose');
  4. var bodyparser = require('body-parser');
  5. var cors = require('cors');
  6. var path = require('path');
  7.  
  8. var app = express();
  9.  
  10. const route = require('./routes/route');
  11.  
  12. //connect to mongodb
  13. mongoose.connect('mongodb://localhost:27017/contactlist');
  14.  
  15. //on connection
  16. mongoose.connection.on('connected', () => {
  17.   console.log('Connected to database mongodb @ 27017')
  18. });
  19.  
  20. //on error
  21. mongoose.connection.on('error', (err) => {
  22.   console.log('Error in db connection : ' + err);
  23. })
  24.  
  25. //port number
  26. const port = 3000;
  27.  
  28. //adding middleware - cors
  29. app.use(cors());
  30. app.use(bodyparser.json());
  31.  
  32. //static files
  33. //__dirname refers to current direcory
  34. app.use(express.static(path.join(__dirname, 'public')));
  35.  
  36.  
  37. app.use('/', route);
  38. //testing server
  39. app.get('/', (req, res) => {
  40.   res.send('Foobar');
  41. })
  42.  
  43. app.listen(port, () => {
  44.   console.log('Server started at port ' + port);
  45. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top