Express Js Snippets

PHOTO EMBED

Sun Oct 06 2024 06:28:59 GMT+0000 (Coordinated Universal Time)

Saved by @maclaw

// Require Express
const express = require("express");

//require path for pathname in the folder 
// __dirname is the directory name
// path.join(__dirname, *path in folder*)
const path = require('path');

//get method
//get (*url path*, ())
app.get('*url path*', (req, res) => {
  //for sendingFile (rendering the file)
  res.sendFile(path.join(_dirname, 'sample.html'))
})

//get with json
app.get ('*url path*', (req, res) => {
  //with status
  res.status(404).json({msg: 'Page not found 404l;'})
  res.status(200).json({*json object to response*})
})
content_copyCOPY