asp.net consultancy chorley ASP.NET Server Side Scripting Wigan
Website Development Radcliffe

A Coder's Notes

Email : ren@techsolus.co.uk
bespoke invoice systems Standish Information Technology Advice Worsley
    development systems Ramsbottom
data manipulation Farnworth access databases Whitefield SQL connections Atherton
online accessible software Leyland software compatible Coppull


Get connected and Online Tottington
remote management Makerfield code and design Aspull

NodeJS & Express Routing Basics - 10/05/2018

So Node is NOT a web server. Express makes it into a sort of serviceable web server. Here's a routing page for NodeJS and Express.

 

Start with...

const express = require('express')
const app = express()
 
This is like calling express which you'll have to install. Why then you have to rename express to app no-one seems to bloody well know but just do it...right!
 
app.use(express.json({ type: 'application/json' }));
 
If you want to parse any JSON you'll need this. Again bloody complex and be careful Express has it's own JSON parser after 4.16 or something. Before that you had to install bodyparse along with Express. Holy cow it's likes working with Linux kernals...
 
app.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
  res.header('Access-Control-Allow-Credentials', 'true');
  res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Auth');
  next();
})
 
This lot tells express to send out headers. This sort of tells browsers what they can and cannot do. I don't see what the point is because a real hacker will send what they hell they want. Anyhow that's what they want...pffft
 
app.route('/getnames')
  .get(async (req, res) => {CODE HERE CODE HERE})
  .put(async (req, res) => {CODE HERE CODE HERE})
 
So if the request is to "http://mysite.com/getnames then the code  between the curly bits gets run. If the request is a GET then the .get code gets run. So erm logically if it's a PUT then the put code gets run
 
app.route('/getnames?myid=123')
  .get(async (req, res) => {CODE HERE CODE HERE using req.query.myid})
  .put(async (req, res) => {CODE HERE CODE HERE using req.query.myid})
 
We can use the query the bit after the question mark for passed data. To make matters more complex there's another way of passing data in the URL
 
app.route('/getnames/:thisname?something=thi')
.get(async (req, res) => {
  req.body - // what inthe body ie JSON
  req.params - //the bit after the : colon ie request.params.thisname
  req.query - the bit after the ? req.query.something
})
.post(async (req, res) => {
  req.body - what inthe body ie JSON
  req.params - the bit after the : colon ie request.params.thisname
  req.query - the bit after the ? req.query.something
})
 
So now we have a colon for PARAMS, a question mark for QUERY and the actual URL too. THEN THEN THEN there's the body
 
app.put('/update', async (req, res) => {
  res.send('EEE' + req.body.xxx)
 console.log(req.body)
})
 
 
 

 

Post A Comment

Name Comment
programming services asp.net specialist
Valid XHTML 1.0 Transitional
Admin
GD