Skip to content

Instantly share code, notes, and snippets.

@dineshdeveloper1
Last active October 16, 2023 17:01
Show Gist options
  • Save dineshdeveloper1/0d2f024187eee06580fc40ed1041c884 to your computer and use it in GitHub Desktop.
Save dineshdeveloper1/0d2f024187eee06580fc40ed1041c884 to your computer and use it in GitHub Desktop.
express
const express = require('express')
const app = express(); // execute express
app.get('', (req, res)=>{ // get or post method
res.send('Hello, Sunday School -- home page')
})
app.get('/about', (req, res)=>{
res.send('Hello, Sunday School -- about page')
})
app.listen(8080)
============================================================================
// add html data
app.get('/about', (req, res)=>{
res.send(`
<a href='/about'>About</a>
<h2>Hello, Sunday School -- about page<h2>
<input type='text' placeholder="Name" value="${req.query.name}"> // localhost:8080/about?name=Sunday
`)
})
//show json data in object
app.get('/contact', (req, res)=>{
res.send({
name: 'Sunday School',
email: 'contact@sundayschool.co.in'
})
})
//show json data in array object
app.get('/contact', (req, res)=>{
res.send([
{
name: 'Sunday School',
email: 'contact@sundayschool.co.in'
},
{
name: 'Sunday School',
email: 'contact@sundayschool.co.in'
},
])
})
// Make a simple html website /////////////////////////////////////////////////////////////////////////////////////////////
// create about.html page > crate public folder and add about.html (or may create multiple pages)
const express = require('express')
const path = require('path') // help to access the folder
const app = express()
const publicPath = path.join(__dirname, 'public')
app.use(express.static(publicPath)) // use is express js function, staic - load static page
app.listen(5000)
//remove extension from page or redirect the page, create 404 page
const express = require('express')
const path = require('path') // help to access the folder
const app = express()
const publicPath = path.join(__dirname, 'public')
app.get('',(req, res) =>{
res.sendFile(`${publicPath}/index.html`)
})
app.get('/aboutus',(req, res) =>{
res.sendFile(`${publicPath}/about.html`)
})
app.get('*',(req, res) =>{
res.sendFile(`${publicPath}/404.html`) // 404 page (page not found)
})
app.listen(5000)
// template engine - EJS -Embedded JavaScript templates (for createing dynamic pages) ------------------------------
//install - https://www.npmjs.com/package/ejs
app.set('view engine', 'ejs')
app.get('/profile', (req, res) =>{
const user = {
name: "Preeti",
skills: ['html', 'javascript']
}
res.render('profile', {user})
})
//folder - views - profile.ejs page
<h1>Hi <%= user.name %>, Welcome to Instagram</h1>
<% user.skills.forEach((item) => { %>
<%= item %>
<%}) %>
// common file--------------------------------------------
//views/common - header.ejs
<nav>This is header and Nav</nav>
//views - profile.ejx
<%- include('common/header') %>
//index.html
app.get('/header', (req, res) => {
res.render('header');
})
// middleware --------------------------------------------------------------------------
var express = require('express');
var app = express();
const reqFilter = (req, res, next) => {
if(!req.query.age){
res.send('Please provide your age')
}else if(req.query.age < 18){
res.send('You can not access this website')
}else{
next()
}
}
//check resule http://localhost:8080/?age=5
app.use(reqFilter)
app.get('/', (req, res)=> {
res.send('welcome to homepage')
});
app.get('/about', (req, res)=> {
res.send('welcome to about page')
});
app.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment