node express request get route code example

Example 1: express request path

// GET 'http://www.example.com/admin/new?a=b'
app.get('/admin', (req, res, next) => {
  req.originalUrl; // '/admin/new?a=b' (full path with query string)
  req.baseUrl; // '/admin'
  req.path; // '/new'
  req.baseUrl + req.path; // '/admin/new' (full path without query string)
});

Example 2: 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');
});