This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
app.get('/', (req,res)=> { //get method | |
res.send('Hello World') //send response | |
}) | |
app.listen(3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/',(req,res)=>{ | |
console.log("at root") | |
}) | |
app.get('/hussain', (req,res)=>{ | |
console.log('at url named: Hussain'); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)=>{ // |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
var app = express() | |
//declare middleware first | |
var myLogger = function (req, res, next) { | |
console.log('LOGGED') | |
next() | |
} | |
//now use it |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
var app = express() | |
var requestTime = function (req, res, next) { | |
req.requestTime = Date.now() | |
next() | |
} | |
app.use(requestTime) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)=>{ |
OlderNewer