i created a server with node js on server.js. but i have problem with my code. here is my full code on server.js

const mysql = require('mysql');
const express = require('express');
var app = express();
const bodyparser = require('body-parser');

app.use(bodyparser.json);

var mysqlConnection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'nodejs_employee'
});

mysqlConnection.connect((err) => {
  if (!err) {
    console.log('Db conected');
  }
  else{
    console.log('not connect');
  }
});

app.listen(3000, () => console.log('Express server running'));

app.get('/employees', (req,res) => {
  mysqlConnection.query("SELECT * FROM employeedb", function (err, result, fields) {
    if(err){
      throw err;
    }
    console.log(result);
  });
});

Look at the block code like this

mysqlConnection.query("SELECT * FROM employeedb", function (err, result, fields) {
    if(err){
      throw err;
    }
    console.log(result);
  });

that mysql.connection.query code doesn't work when inside app.get. But when I comment to the app.get line (open tag and close tag), it works. I have no idea to fix it. Please help me

1 Answers

0
Hayk Aghabekyan On Best Solutions

Instead of line 6 where you have written

app.use(bodyparser.json);

Use

app.use(bodyparser.json());

I hope it should help