快递js路线停止工作,发送给我无法得到任何回应

时间:2018-06-10 11:08:25

标签: javascript node.js express

我的快递旧路线正在工作,除了一条路线和我正在制作的新路线不起作用无法得到任何回应每当我在邮递员上点击它时。

Sample response image

这是我的app.js文件:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var helpers = require("./helpers/")();
var index = require('./routes/index');
var users = require('./routes/users');
var purchase = require('./routes/purchase');
var driver = require('./routes/driver');
var notification = require('./routes/notification');
var admin = require('./routes/admin');
var test = require('./routes/test');

var app = express();

mongoose.connect("mongodb://localhost/myapp");
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//file upload


app.use(function (req, res, next) {

  var oldWrite = res.write;
  var oldEnd = res.end;
  var chunks = [];
  // models.Logger.req = req;
  res.write = function (chunk) {

    chunks.push(chunk);
    oldWrite.apply(res, arguments);
  };
  res.end = function (chunk) {
    if (chunk) chunks.push(chunk);
    if (helpers.general.count(chunk) < 2) {
      var buff100 = new Buffer(100);
      chunks.push(buff100);
    }
    if (typeof chunks == "object") {
      console.log(res.statusCode);
      if (
        res.statusCode != 401 &&
        res.statusCode != 404 &&
        res.statusCode != 302 &&
        res.statusCode != 500
      ) {
        var body = Buffer.concat(chunks).toString("utf8");
        console.log(body);
        var bstatus = JSON.parse(body);
        if(!bstatus.status){
          res.statusCode = 422;
        }
        // models.Logger.req = req;
        // models.Logger.logInternal(req, body);
      }
    }

    if (res.statusCode == 401) {
      var unauth = JSON.stringify({
        'status': 'false',
        'message': 'Invalid Reseller Credentials'
      });
      res.setHeader('content-type', 'application/json;')
      res.setHeader('charset', 'utf-8')
      arguments[0] = unauth;
      // models.Logger.logInternal(req, unauth);
    }

    oldEnd.apply(res, arguments);
  };
  next();
});

app.use('/', index);
app.use('/users', users);
app.use('/purchase', purchase);
app.use('/driver', driver);
app.use('/notification', notification);
app.use('/admin', admin);
app.use('/test', test);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

这是我的简单路由文件,也无法正常工作

var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
    res.json({ title: 'Express' });
  });
module.exports = router;

当我尝试使用这个简单的测试路线时,它发送给我无法得到任何响应。即使是我的npm开始也不会破坏,甚至不会向我显示任何错误。我也使用pm2,它也没有任何错误跟踪。我还在vscode上尝试了节点调试器,通过添加断点仍然无法找到任何可以帮助我解决这个问题的错误。

这是我的www档案:

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('vero:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3001');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

1 个答案:

答案 0 :(得分:0)

确保您的应用正在运行。调试代码并在listening事件侦听器中放置断点,或者只是在控制台中检查调试打印。

您还可以在终端窗口中运行netstat并检查正在侦听应用程序端口的套接字。示例输出看起来像这样(参见表格的第一行):

maxpaj@maxpaj-VirtualBox:~/code/app$ netstat -ntlp
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:5001            0.0.0.0:*               LISTEN      2829/node
tcp        0      0 127.0.0.1:28594         0.0.0.0:*               LISTEN      2829/node
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:8443            0.0.0.0:*               LISTEN      2829/node
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      -
tcp6       0      0 ::1:631                 :::*                    LISTEN      -

确保您找到正确的地址和路线。检查地址,端口和路由的拼写。可能就这么简单。我在你的示例图片中看到它表示你试图在 locahost 上找到一条路线(拼写错误 - 你在本地错过了一个L)。这可能是问题吗?