Skip to content

Instantly share code, notes, and snippets.

View HussainArif12's full-sized avatar
🏠
Working from home

Hussain Arif HussainArif12

🏠
Working from home
View GitHub Profile
var http = require('http');
const port = 3000
//create a server object:
http.createServer(function (req, res) {
res.writeHead(200,{'Content-Type':'text/html',
'Content-Length':body.length})
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
})
.listen(port); //the server object listens on port 3000
const http = require("http");
const port = 3000
const Server = http.createServer((request, response) => {
response.writeHead(200, {
'Content-Type': 'text/html'})
response.end('Hello World \n ');
console.log(request.headers);
console.log(request.method);
const http = require("http");
const port = 3000
const Server = http.createServer((request, response) => {
if (request.method == 'POST') {
let buff = '' //buffer variable to save response
request.on('data', function (chunk) {
buff += chunk; //concat each newline to the buff variable
})
request.on('end', function () {
@HussainArif12
HussainArif12 / readingJSONArray.js
Created April 22, 2020 16:46
Reads Json Arrays
//json array : [{name : '' } , {name : ' '} , {name : '' } .. }
//Notes is a model in Mongoose
Notes.find({}).exec((err,document)=> {
if(err) console.log(err);
let data = '';
document.forEach((value) => {
data += (value.title + ' : ' +value.description + '\n')
})
res.send(data);
})
@HussainArif12
HussainArif12 / express-basic-hello-world.js
Last active April 25, 2020 06:42
Hello World in Express JS using GET method
const express = require('express')
const app = express()
app.get('/', (req,res)=> { //get method
res.send('Hello World') //send response
})
app.listen(3000)
@HussainArif12
HussainArif12 / express-routing-example.js
Last active April 25, 2020 06:24
Express Routing in GET
app.get('/',(req,res)=>{
console.log("at root")
})
app.get('/hussain', (req,res)=>{
console.log('at url named: Hussain');
})
@HussainArif12
HussainArif12 / express-POST-example.js
Last active February 15, 2022 21:33
Simple POST example in express
const express= require('express')
const app = express()
app.use(express.text())
app.post('/',(req,res)=>{ //POST request submitted at root page
console.log("request received at URL of root")
console.log(req.body)
res.send("data received")
})
app.post('/hussain', (req,res)=>{ //
var express = require('express')
var app = express()
//declare middleware first
var myLogger = function (req, res, next) {
console.log('LOGGED')
next()
}
//now use it
var express = require('express')
var app = express()
var requestTime = function (req, res, next) {
req.requestTime = Date.now()
next()
}
app.use(requestTime)
@HussainArif12
HussainArif12 / request-api-key.js
Created April 27, 2020 09:20
validate URL in express
app.use((req, res, next) => {
if (req.query.api_key) {
next();
} else {
res.status(401).send({ msg: " Data is unauthorized. NO API PRESENT" });
}
})
app.get('/', (req,res,next)=>{