1var express = require('express')
2var router = express.Router()
3
4// middleware that is specific to this router
5router.use(function timeLog (req, res, next) {
6 console.log('Time: ', Date.now())
7 next()
8})
9// define the home page route
10router.get('/', function (req, res) {
11 res.send('Birds home page')
12})
13// define the about route
14router.get('/about', function (req, res) {
15 res.send('About birds')
16})
17
18module.exports = router
19
1const express = require('express');
2const mysql = require('mysql');
3
4// Connecting with database
5const db = mysql.createConnection({
6 host: 'localhost', // The host you're using
7 user: 'yourusername', // The username you use to enter database
8 password: 'yourpassword' // Your password to your username
9});
10
11db.connect((error) => {
12 if(error) {
13 throw error;
14 }
15 console.log('MySQL Connected');
16});
17
18const app = express();
19
20app.get('yourroute', (request, response) => {
21 let sql = 'SELECT * FROM yourtable';
22 let query = db.query(sql, (error, result) => {
23 if(error) {
24 throw error;
25 }
26 console.log(result) // Use the result you get back here
27 })
28});
29
30app.listen('3000', () => {
31 console.log('Server is listening on port 3000');
32});
33
34
1// You need to install the following packages
2npm install --save mysql express
3// And if you don't want to restart your server after every little change
4npm install -g nodemon