Basic Express Server

Connected Devices

01 Feb 2019

To complete the requirements of the assignment I wrote a basic express server. When someone makes a connection to the server, my server prints out their user agent. Also <ipaddress>/browser will show the client their header information.

Things I learned is that you can get the user agent with this syntax: request.headers[‘user-agent’]

res.send() sends an http response

res.write() writes to stream and then res.end() to end response as a chunk.

Practically you can only res.send() once as it is equal to res.write() + res.end()

ref: Omar Perera.

You can visit /browser to learn about your browser

You can visit /browser/{browser} to learn about browsers. ex: localhost:/browser/chrome

const express = require('express');
const app = express();

let ua;
app.get('/', (request, response)=> {
	response.write('<html>');
	response.write('<head>');
	response.write('</head>');
	response.write('<body>');
	response.write('<h1>HELLO!</h1>');
	response.write('go to \'\/browser and I will tell you about yourself ;)')
    response.write('go to \'\/browser\/{your browser} and I will tell you more browsers. ;)')
	response.write('</body>');
	response.write('</html>');
	response.end();
	console.log(request.headers['user-agent']+ ' joined!');
});

app.get('/browser', (request, response)=>{
	ua =request.headers['user-agent'];	
	response.write('<html>');
	response.write('<head></head');
	response.write('<body>');
	response.write('<h1>You\'re using: </h1>'+ ua);
	response.end();
	console.log(ua +' wants to know their browser');
});

app.get('/browser/:browser', (request,response)=>{
	let browser = request.params.browser;
	if(browser!='internetexplorer'){
		response.send("Why don't you try internetexplorer? You know its the most popular web browser.")
	} else {
		response.send("IE #1");
	}
});
app.listen(3000, ()=> console.log('Listening on port 3000'));