无法使用Postman创建用户-Nodemon应用程序崩溃

时间:2018-11-19 03:49:24

标签: javascript node.js express postman

js并处于学习阶段。我试图通过使用mongodb将新用户保存到数据库中来创建新用户。但是,当我尝试使用POSTMAN发布数据时,出现以下错误:

无法得到任何回应。连接到http://localhost:3000/create-user

时出错

Nodemon也崩溃,显示[nodemon]应用程序崩溃-等待文件更改,然后再启动...

我手动运行文件并进行了检查,但是当我尝试发布数据时,连接丢失并终止了。

下面是server.js文件:

const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');

var User = require('./models/user');

const app = express();

mongoose.connect('mongodb://test:check123@ds211694.mlab.com:11694/parpet',  { useNewUrlParser: true }, (err) => {
    if(err) {
        console.log(err);
    } else {
        console.log('Connected Successfully');
    }
})
// Middleware for log data
app.use(morgan('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.post('/create-user', (req, res) => {
    var user = new User();
    user.profile.name = req.body.name;
    user.email = req.body.email;
    user.password = req.body.password;
    user.save(function (err) {
        console.log('checking.....');
        if(err) {
            console.log('Error');
        } else {
            res.json('Successfully created a new user');
        }
    });
});

const port = process.env.PORT  || 3000;

app.listen(port, (err) => {
    if(err) throw err;
    console.log(`Server listening on port ${port}`);
});

模型文件是

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');

var Schema = mongoose.Schema;

/* Creating User Schema */
var UserSchema = new Schema({
    email: {
        type: String,
        unique: true,
        lowercase: true
    },
    password: String,
    profile: {
        name: {
            type: String,
            default: ''
        },
        picture: {
            type: String,
            default: ''
        }
    },
    address: String,
    history: [{
        date: Date,
        paid: {
            type: Number,
            default: 0
        },
        // item: 
    }]
});

/* Hash the password */
UserSchema.pre('save', function (next) {
    var user = this;
    if(!user.isModified('password')) {
        return next();
    }
    bcrypt.genSalt(10, function (err, salt) {
        if(err) return next(err);
        bcrypt.hash(user.password, salt, null, function (err, hash) {
            if(err) return next(err);
            user.password = hash;
            next();
        });
    });
});

/* Comparing typed password with that in DB */
UserSchema.methods.comparePassword = function (password) {
    return bcrypt.compareSync(password, this.password);
}

module.exports = mongoose.model('User', UserSchema);

我尝试禁用其他stackoverflow问题中发布的ssl连接,但这没有帮助。

所以有人可以帮我解决这个问题。真的很有帮助。

node --version    = v8.9.3
npm --version     = 6.4.1
nodemon --version = 1.17.5
express --version = 4.16.0

谢谢

2 个答案:

答案 0 :(得分:1)

似乎对bcrypt的调用正在悄然崩溃:

library(dplyr)
library(tidyr)
library(NPS)
set.seed(123)

# data
df <- data.frame(score = sample(c(0:10), 15, replace = TRUE),
                 variable = sample(c('A', 'B', 'C'), 15, replace = TRUE))

# custom function
analyzer <- function(df, var, sco) {
  var <- rlang::ensym(var)
  sco <- rlang::ensym(sco)

  df <- df %>% 
    group_by(., !!rlang::enquo(var)) %>% 
    transmute(., n = NPS::nps(!!rlang::enquo(sco))) %>% 
    unique()

  return(df)
}

# using function
analyzer(df, 'variable', 'score')
#> # A tibble: 3 x 2
#> # Groups:   variable [3]
#>   variable      n
#>   <fct>     <dbl>
#> 1 C        -0.333
#> 2 A        -0.4  
#> 3 B        -0.25

我不确定bcrypt.hash(user.password, salt, null, function (err, hash) { 参数的来源。如果将其删除:

null

它不再崩溃,也不返回错误。

答案 1 :(得分:0)

我遇到了同样的问题,通过将bcrypt包降级为2.0.1解决了该问题。