【问题标题】:http://localhost:3000/api/users net::ERR_CONNECTION_REFUSEDhttp://localhost:3000/api/users net::ERR_CONNECTION_REFUSED
【发布时间】:2016-07-21 16:07:18
【问题描述】:

我可以登录我的应用程序并显示登录成功,但是当我刷新页面时出现此错误

http://localhost:3000/api/users net::ERR_CONNECTION_REFUSED

我在前面使用带有 Angular 的 Node.js。也将 JWT 用于令牌。

我的后端 app.js:

    app.use(morgan('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(cookieParser());

mongoose.connect(config.database);

app.use(cors({origin: "http://localhost:8000",credentials: true }));

app.use('/api/upload/single', expressJWT({secret: config.secret}));



app.use(function (error, request, response, next) {   if (error.name === 'UnauthorizedError') {
    response.status(401).json({message: 'You need an authorization token to view this page.'});   } });

// MULTER var s3config = require('./config/s3'); var upload = multer({ storage: s3({
    dirname: s3config.dirname,
    bucket: s3config.bucket,
    secretAccessKey: process.env.AWS_ACCESS_SECRET,
    accessKeyId: process.env.AWS_ACCESS_KEY,
    region: s3config.region,
    contentType: function(req, file, next) {
      next(null, file.mimetype);
    },
    filename: function(req, file, next) {
      var ext = '.' + file.originalname.split('.').splice(-1)[0];
      var filename = uuid.v1() + ext;
      next(null, filename);
    }   }) });

// UPLOAD SINGLE FILE app.post('/api/upload/single', upload.single('file'), function(req, res) {   var clothing_1 = {
    type: req.body.type,
    image: req.file.key   };   console.log(req.body);   // get the user model User.findOne({ id: req.user._id })   User.findOne({ _id: req.user._id }, {}, { new: true }, function(err, user){
    user.clothing.push(clothing_1);
    // save this user
    user.save(function(err, user){
      if(err) return res.status(401).send({ message: 'your error:' + err });
      else return res.json({ user: user })
    });   });

}); // app.get('api/users', usersController.showUser); app.use('/api', routes);

app.listen(3000);

【问题讨论】:

  • 请发布您的服务器端代码以帮助我们进行故障排除。否则无法回答。
  • ERR_CONNECTION_REFUSED 错误表明它没有运行,因此可能已经崩溃。放入一些日志记录或使用节点检查器进行调试以查看导致崩溃的原因

标签: node.js api express


【解决方案1】:

结果只是没有在我的 Angular 状态上放置正确的控制器。

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2021-03-30
    • 1970-01-01
    • 2021-03-11
    • 2022-09-28
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多