express js routing application code example

Example 1: nodejs express routing get

const express = require('express');
const mysql = require('mysql');

// Connecting with database
const db = mysql.createConnection({
  host: 'localhost',					// The host you're using
  user: 'yourusername',					// The username you use to enter database
  password: 'yourpassword'				// Your password to your username
});

db.connect((error) => {
  if(error) {
    throw error;
  }
  console.log('MySQL Connected');
});

const app = express();

app.get('yourroute', (request, response) => {
  let sql = 'SELECT * FROM yourtable';
  let query = db.query(sql, (error, result) => {
    if(error) {
      throw error;
    }
    console.log(result)					// Use the result you get back here
  })
});

app.listen('3000', () => {
  console.log('Server is listening on port 3000');
});

Example 2: express basic routing syntax

app.METHOD(PATH, HANDLER)