Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var app = express();
  3.  
  4. app.set('view engine', 'jade');
  5.  
  6. var mysql = require('mysql');
  7. var connection = mysql.createConnection({
  8.     host: 'localhost',
  9.     user: 'root',
  10.     password: ',fhf,fy86',
  11.     database: 'hello-world-express'
  12. });
  13. connection.connect();
  14.  
  15.  
  16. app.get('/', function (req, res) {
  17.     connection.query('SELECT * FROM post ORDER BY id DESC LIMIT 10', function (err, rows) {
  18.         res.render('index', {posts: rows});
  19.     });
  20. });
  21.  
  22. app.get('/category/:id', function (req, res) {
  23.     var id = req.params.id;
  24.  
  25.     connection.query('SELECT * FROM category WHERE id = ?', [id], function (err, rows) {
  26.         var category = rows[0];
  27.  
  28.         connection.query('SELECT * FROM post WHERE category_id = ?', [id], function (err, rows) {
  29.             res.render('category', {category: category, posts: rows});
  30.         });
  31.     });
  32. });
  33.  
  34. app.get('/post/:id', function (req, res) {
  35.     connection.query('SELECT * FROM post WHERE id = ?', [req.params.id], function (err, rows) {
  36.         res.render('post', {post: rows[0]});
  37.     });
  38. });
  39.  
  40. app.listen(3000);
  41.  
  42.  
  43. //connection.end();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement