Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Server started on port 3000
- Database Error MongoError: failed to connect to server [localhost:27017]
- on first connect
- TypeError: User is not a constructor
- at router.post (/Users/user/Desktop/Development/meanauthapp/routes/users.js:12:16)
- 2017-03-15T09:52:49.306-0700 I NETWORK
- [thread1] waiting for connections on port 27017
- 2017-03-15T09:52:54.514-0700 I NETWORK
- [thread1] connection accepted from 127.0.0.1:49188 #1 (1 connection now open)
- 2017-03-15T09:52:54.515-0700 I NETWORK
- [conn1] received client metadata from 127.0.0.1:49188 conn1:
- { application: { name: "MongoDB Shell" },
- driver: { name: "MongoDB Internal Client", version: "3.4.1" },
- os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "15.6.0" } }
- /*---------------Dependencies-------------*/
- const express = require('express');
- const router = express.Router();
- const User = require('../config/database');
- const passport = require('passport')
- const jwt = require('jsonwebtoken');
- /*----------------------------------------*/
- /*---------------Register-----------------*/
- router.post('/register', (req, res, next) => {
- let newUser = new User({
- name: req.body.name,
- email: req.body.email,
- username: req.body.username,
- password: req.body.password
- });
- User.addUser(newUser, (err, user) => {
- if(err) {
- res.json({success: false, msg:'Failed to register user'});
- } else {
- res.json({success: true, msg: 'User Registered'})
- }
- });
- });
- /*----------------------------------------*/
- /*---------------Authenticate---------------*/
- router.post('/authenticate', (req, res, next) => {
- res.send('AUTHENTICATE')
- });
- /*----------------------------------------*/
- /*-----------------Profile------------------*/
- router.get('/profile', (req, res, next) => {
- res.send('PROFILE')
- });
- /*----------------------------------------*/
- module.exports = router;
- module.exports = { database: "mongodb://localhost:27017/famjam",
- secret : "yoursecret" }
- const mongoose = require('mongoose');
- const bcrypt = require('bcryptjs');
- const config = require('../models/database');
- /*User Scheme*/
- const UserScheme = mongoose.Scheme({
- name: {
- type: String
- },
- email: {
- type: String,
- required: true
- },
- username: {
- type: String,
- required: true
- },
- password: {
- type: String,
- required: true
- }
- });
- const User = module.exports = mongoose.model('User', UserScheme);
- module.exports.getUserById = function(id, callback) {
- User.findById(id, callback);
- }
- module.exports.getUserbyUsername = function(username, callback) {
- const query = {username: username}
- User.findOne(query, callback);
- }
- module.exports.addUser = function(newUser, callback) {
- bcrypt.genSalt(10, (err, salt) => {
- bcrypt.hash(newUser.password, salt, (err, hash) => {
- if(err) throw err;
- newUser.password = hash;
- newUser.save(callback);
- })
- });
- }
- /*---------------Dependencies-------------*/
- const express = require('express');
- const path = require('path');
- const bodyParser = require('body-parser');
- const cors = require('cors');
- const passport = require('passport');
- const mongoose = require('mongoose');
- const users = require('./routes/users')
- const config = require('./config/database')
- /*----------------------------------------*/
- /*---------------Database-------------*/
- mongoose.connect(config.database);
- mongoose.connection.on('connected', () => {
- console.log('connected to database ' +config.database)
- });
- mongoose.connection.on('error', (err) => {
- console.log('Database Error '+err)
- });
- /*----------------------------------------*/
- /*------------------App-------------------*/
- const app = express();
- // Port Number
- const port = 3000;
- app.listen(port, () => {
- console.log('Server started on port '+port)
- });
- //CORS Middleware
- app.use(cors());
- // Body Parser Middelware
- app.use(bodyParser.json())
- // Set Static Folder
- app.use(express.static(path.join(__dirname, 'public')));
- app.use('/users', users)
- /*----------------------------------------*/
- /*---------------Index Route--------------*/
- app.get('/', (req, res) => {
- res.send('Invalid Endpoint')
- });
- app.get('/myaccount', (req,res) =>{
- res.render('myaccount')
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement