0
ルートコードAsyncJSシリーズで立ち往生特急サーバーNodeJS
router.post('/add', function(req, res) {
const compulsoryFields = ['primary_id', 'secondary_id', 'assessment_note_id', 'packing_list_id', 'model_id']
for(var i = 0; i < compulsoryFields.length; i++) {
if(req.body[compulsoryFields[i]] == '') {
req.flash('warning_msg', 'Please make sure all * fields are supplied')
res.redirect('/rmv/add')
return
}
}
async.series([
function(callback) {
Vehicle.existsByPrimaryId(req.body.primary_id, callback)
}, function(callback) {
Vehicle.existsBySecondaryId(req.body.secondary_id, callback)
}
], function(err, details) {
if(!details[0]) {
if(!details[1]) {
var vehicle = { id: null }
for(var key in req.body) {
vehicle[key] = req.body[key]
}
async.series([
function(callback) {
Vehicle.add(vehicle, callback)
}
], function(err, details_l2) {
if(details_l2[0]) {
req.flash('warning_msg', 'Vehicle added successfully')
res.redirect('/rmv/add')
} else {
req.flash('warning_msg', 'Error occured while adding vehicle. Please try again')
res.redirect('/rmv/add')
}
})
} else {
req.flash('warning_msg', 'Chassis/Frame number already exists')
res.redirect('/rmv/add')
}
} else {
req.flash('warning_msg', 'Engine/Motor number already exists')
res.redirect('/rmv/add')
}
})
})
モデルコード
const MySql = require('../comms/rmv_mysql_con')
const Vehicle = module.exports = {}
Vehicle.add = function(vehicle, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('INSERT INTO vehicle SET ?', vehicle, function(err, result) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, true)
})
})
}
Vehicle.getCount = function(callback) {
MySql.pool.getConnection(function(pool_err, connection) {
connection.query('SELECT COUNT(*) AS \'vehicle_count\' FROM vehicle', function(err, rows) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, rows[0]['vehicle_count'])
})
})
}
Vehicle.search = function(skw, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT vehicle.*, primary_name, secondary_name, name FROM vehicle, model WHERE vehicle.model_id = model.id AND CONCAT(model_id, packing_list_id, assessment_note_id, primary_id, secondary_id, sale_date, customer_first_name, customer_last_name, customer_phone_number, customer_address_no, customer_address_street, customer_address_district, customer_nic, rmv_documents_received_date, rmv_documents_sent_date, cr_received_date, plate_number, plate_sent_date, cr_sent_date, remarks) LIKE \'%' + skw + '%\'', function(err, rows, fields) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, rows)
})
})
}
Vehicle.existsByPrimaryId = function(id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT * FROM vehicle WHERE primary_id = ?', id, function(err, rows, fields) {
connection.release()
if(rows.length > 0) {
return callback(err, true)
}
callback(err, false)
})
})
}
Vehicle.getAll = function(callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT vehicle.*, primary_name, secondary_name, name FROM vehicle, model WHERE vehicle.model_id = model.id', function(err, rows, fields) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, rows)
})
})
}
Vehicle.updateById = function(vehicle, id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('UPDATE vehicle SET ? WHERE id = ?', [vehicle, id], function(err, result) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, true)
})
})
}
Vehicle.getVehicleById = function(id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT vehicle.id, primary_id, sale_date, customer_first_name, customer_last_name, customer_phone_number, customer_address_no, customer_address_street, customer_address_district, customer_nic, rmv_documents_received_date, rmv_documents_sent_date, cr_received_date, plate_number, plate_sent_date, cr_sent_date, remarks, primary_name, secondary_name FROM vehicle, model WHERE vehicle.model_id = model.id AND vehicle.id = ?', id, function(err, rows, fields) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, rows)
})
})
}
Vehicle.existsBySecondaryId = function(id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT * FROM vehicle WHERE secondary_id = ?', id, function(err, rows, fields) {
connection.release()
if(rows.length > 0) {
return callback(err, true)
}
callback(err, false)
})
})
問題がとき5-10要求し、サーバーがハングの間のどこかになって追加した後、ありますエクスプレスデバッグの出力は次のようになります。
/rmv/add
express:router router : /rmv/add +0ms
express:router dispatching POST /rmv/add +0ms
express:router trim prefix (/rmv) from url /rmv/add +0ms
express:router router /rmv : /rmv/add +0ms
express:router dispatching POST /add +0ms
express:router <anonymous> : /rmv/add +0ms
express:router <anonymous> : /rmv/add +1ms
express:router <anonymous> : /rmv/add +0ms
express:router dispatching GET /rmv/add +134ms
express:router query : /rmv/add +0ms
express:router expressInit : /rmv/add +0ms
express:router jsonParser : /rmv/add +0ms
express:router urlencodedParser : /rmv/add +0ms
express:router cookieParser : /rmv/add +0ms
express:router serveStatic : /rmv/add +0ms
express:router session : /rmv/add +4ms
express:router initialize : /rmv/add +0ms
express:router authenticate : /rmv/add +0ms
express:router <anonymous> : /rmv/add +4ms
express:router <anonymous> : /rmv/add +0ms
/rmv/add
express:router router : /rmv/add +1ms
express:router dispatching GET /rmv/add +0ms
express:router trim prefix (/rmv) from url /rmv/add +0ms
express:router router /rmv : /rmv/add +0ms
express:router dispatching GET /add +0ms
express:router <anonymous> : /rmv/add +0ms
express:router <anonymous> : /rmv/add +0ms
express:router <anonymous> : /rmv/add +0ms
サーバがハングアップすると、私が行うすべてのリクエストは、asyncjを使用しないリクエストを除いてスタックされます。サーバーは正常に動作し、再起動され、ポストルートを追加するための5〜10回の要求を受け取った後に問題が再度発生します。