Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongoose = require('mongoose');
- var path = require('path');
- const express = require('express')
- const app = express();
- var bodyParser = require('body-parser');
- var server = require('http').createServer(app);
- var socketapp = require("socket.io")(server)
- var Schema = mongoose.Schema;
- mongoose.Promise = global.Promise;
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: true }));
- var dburl = 'mongodb://emilpriver:Makrill84@ds119969.mlab.com:19969/emilpriver';
- mongoose.connect('mongodb://emilpriver:Makrill84@ds119969.mlab.com:19969/emilpriver');
- var Schema = mongoose.Schema;
- const router = express.Router();
- var assert = require('assert');
- app.use(express.static('./'));
- var kontakter = new mongoose.Schema({
- namn: String,
- mail: String,
- password: String
- });
- var User = mongoose.model("User", kontakter);
- app.get('/', function(req, res) {
- res.sendFile(path.join(__dirname + '/index.html'));
- });
- app.get('/users', function(req, res){
- res.sendFile(path.join(__dirname + '/users.html'));
- });
- //Hämta users data ifrån databsen
- mongoose.connection.on('error', function() {
- console.log('Could not connect to the database. Exiting now...');
- process.exit();
- });
- mongoose.connection.once('open', function() {
- console.log("Successfully connected to the database");
- })
- //get users
- app.get('/api/users', (req,res,next) => {
- mongoose.connect(dburl, function(err,db){
- var collection = db.collection("users").find({}).toArray(function(err, result) {
- if (err) throw err;
- res.json(result);
- db.close();
- });
- });
- });
- // lägg till användare
- app.post("/addkontakt", (req, res) => {
- var SendKontakter = new User(req.body);
- SendKontakter.save()
- .then(item => {
- res.send("item saved to database");
- })
- .catch(err => {
- res.status(400).send("unable to save to database");
- });
- });
- app.listen(80, () => console.log('Listening to port 80!'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement