JSON output with Express Mysql -


i have mysql database connected. can't seem query data output in console whether there error or not. there wrong way i've set up?

var express = require('express'); var path = require('path'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var routes = require('./routes/index'); var app = express(); var mysql = require('mysql');   var app = express();  var connection = mysql.createconnection({     host: 'localhost',     user: 'root',     password: "root",     database: 'photo_gallery' });  connection.connect();   // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded()); app.use(cookieparser()); app.use(express.static(path.join(__dirname, 'public')));  app.locals.appdata = require('./data.json');  app.use('/', routes);  app.get('/',function(request,response){ connection.query('select title stories', function(err, rows, fields)     {             console.log('connection result error '+err);             console.log('no of records '+rows.length);                     response.writehead(200, { 'content-type': 'application/json'});             response.end(json.stringify(rows));     });  } );     module.exports = app; 

i able parse data mysql json object , use object in express route in index.js file set in folder titled route in main directory.

var express = require('express'); var router = express.router(); var appdata = require('../data.json'); var mysql = require('mysql');   var connection = mysql.createconnection({     host: 'localhost',     user: 'root',     password: "root",     port : 3306, //port mysql     database: 'photo_gallery' });  connection.connect();   router.get('/', function(request,response) {   connection.query('select * stories', function(err, rows, fields) {     if (err) {       console.log('encountered error:', err.message);       return response.send(500, err.message);     }     thedata = ({'services' : rows});     console.log(thedata);      response.render('index', {      title: 'home',     data: thedata,      page: 'home'   });   }); }); 

this snippet index.ejs file makes use of json object.

<a href="/services/"><li>service overview</li></a>    <% thedata.services.foreach(function(item) { %>       <a href="/services/<%= item.title %>"><li><%= item.title %></li></a>   <% }); %> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -