Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

express router file

var express = require('express');
var router = express.Router();

// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
  console.log('Time: ', Date.now());
  next();
});
// define the home page route
router.get('/', function (req, res) {
  res.send('Birds home page');
});
// define the about route
router.get('/about', function (req, res) {
  res.send('About birds');
});

module.exports = router;
Comment

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

Comment

nodejs: router by use express and path package

// use Express 
// use Path

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

const app = express();


// ###Create Public folder
app.use(express.static('./public'));




app.get('/', (req, res) => {
    res.sendFile(path.resolve(__dirname, './navbar-app/index.html'));
});


app.all('*', (req, res) => {
    res.status(404).send('Resource not founded');
})


app.listen(5000, () => {
    console.log('Server is listening on PORT: 5000');
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: add a class in react 
Javascript :: nodejs redis 
Javascript :: knex pagination plugin 
Javascript :: $(...).Datatables is not a function 
Javascript :: jquery traversing methods 
Javascript :: npm fund 
Javascript :: (node:15855) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 
Javascript :: angular set time 
Javascript :: js update query string without refresh 
Javascript :: Find items from object 
Javascript :: how to compile javascript 
Javascript :: skip map iteration javascript 
Javascript :: fs readfile promise 
Javascript :: class 
Javascript :: divisible check javascript 
Javascript :: arrow function in es6 
Javascript :: byte array to json 
Javascript :: array iterator javascript 
Javascript :: binary search tree js 
Javascript :: autocomplete html in react 
Javascript :: The ".charAt()" JavaScript string method 
Javascript :: deploy react and express to heroku 
Javascript :: laravel json eloquent 
Javascript :: lazy loading by scroll vue 
Javascript :: binarysearch 
Javascript :: jqvmap 
Javascript :: javascript for...in loop 
Javascript :: reverse integer in for javascript 
Javascript :: object object js 
Javascript :: js then vs await 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =