Running nodejs application. I received this error: I ran my application with the command node app.js
Versions:
node: 8.9.2
npm: 5.5.1
express: 4.15.5
App.jscode:
/***Moduledependencies.*/varexpress=require('express');varroutes=require('./routes');varhttp=require('http');varpath=require('path');//loadcustomersroutevarcustomers=require('./routes/customers');varapp=express();varconnection=require('express-myconnection');varmysql=require('mysql');//allenvironmentsapp.set('port',process.env.PORT||4300);app.set('views',path.join(__dirname,'views'));app.set('viewengine','ejs');//app.use(express.favicon());app.use(express.logger('dev'));app.use(express.json());app.use(express.urlencoded());app.use(express.methodOverride());app.use(express.static(path.join(__dirname,'public')));//developmentonlyif('development'==app.get('env')){app.use(express.errorHandler());}/*------------------------------------------connectionpeer,registerasmiddlewaretypekoneksi:single,poolandrequest-------------------------------------------*/app.use(connection(mysql,{host:'localhost',user:'root',password:'',port:3306,//portmysqldatabase:'nodejs'},'pool')//orsingle);app.get('/',routes.index);app.get('/customers',customers.list);app.get('/customers/add',customers.add);app.post('/customers/add',customers.save);app.get('/customers/delete/:id',customers.delete_customer);app.get('/customers/edit/:id',customers.edit);app.post('/customers/edit/:id',customers.save_edit);app.use(app.router);http.createServer(app).listen(app.get('port'),function(){console.log('Expressserverlisteningonport'+app.get('port'));});
codecustumer.ejs
<%-includelayouts/header.ejs%><divclass="page-data">
<div class="data-btn">
<button onClick="addUser();">+ Add</button>
</div>
<div class="data-table">
<table border="1" cellpadding="7" cellspacing="7">
<tr>
<th width="50px">No</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>Email</th>
<th width="120px">Action</th>
</tr>
<% if(data.length){
for(var i = 0;i < data.length;i++) { %>
<tr>
<td><%=(i+1)%></td>
<td><%=data[i].name%></td>
<td><%=data[i].address%></td>
<td><%=data[i].phone%></td>
<td><%=data[i].email%></td>
<td>
<a class="a-inside edit" href="../customers/edit/<%=data[i].id%>">Edit</a>
<a class="a-inside delete" href="../customers/delete/<%=data[i].id%>">Delete</a>
</td>
</tr>
<% }
}else{ %>
<tr>
<td colspan="3">No user</td>
</tr>
<% } %>
</table>
</div>
</div>
<%- include layouts/footer.ejs %>
costumer.js code
/*
* GET customers listing.
*/
exports.list = function(req, res){
req.getConnection(function(err,connection){
connection.query('SELECT * FROM customer',function(err,rows) {
if(err)
console.log("Error Selecting : %s ",err );
res.render('customers',{page_title:"Customers - Node.js",data:rows});
});
});
};
exports.add = function(req, res){
res.render('add_customer',{page_title:"Add Customers-Node.js"});
};
exports.edit = function(req, res){
var id = req.params.id;
req.getConnection(function(err,connection){
connection.query('SELECT * FROM customer WHERE id = ?',[id],function(err,rows)
{
if(err)
console.log("Error Selecting : %s ",err );
res.render('edit_customer',{page_title:"Edit Customers - Node.js",data:rows});
});
});
};
/*Save the customer*/
exports.save = function(req,res){
var input = JSON.parse(JSON.stringify(req.body));
req.getConnection(function (err, connection) {
var data = {
name : input.name,
address : input.address,
email : input.email,
phone : input.phone
};
var query = connection.query("INSERT INTO customer set ? ",data, function(err, rows)
{
if (err)
console.log("Error inserting : %s ",err );
res.redirect('/customers');
});
// console.log(query.sql); get raw query
});
};/*Save edited customer*/
exports.save_edit = function(req,res){
var input = JSON.parse(JSON.stringify(req.body));
var id = req.params.id;
req.getConnection(function (err, connection) {
var data = {
name : input.name,
address : input.address,
email : input.email,
phone : input.phone
};
connection.query("UPDATE customer set ? WHERE id = ? ",[data,id], function(err, rows)
{
if (err)
console.log("Error Updating : %s ",err );
res.redirect('/customers');
});
});
};
exports.delete_customer = function(req,res){
var id = req.params.id;
req.getConnection(function (err, connection) {
connection.query("DELETE FROM customer WHERE id = ? ",[id], function(err, rows)
{
if(err)
console.log("Error deleting : %s ",err );
res.redirect('/customers');
});
});
};