Skip to content
Snippets Groups Projects
Commit 32704263 authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

add intro server/api code

parent 9483425e
Branches
No related tags found
No related merge requests found
Showing
with 237 additions and 0 deletions
extends layout
block content
h1= title
p Welcome to #{title}
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
# Steps to set-up express application locally
```npm install express-generator -g```
run `express --help`
run `express first-express-app --view=pug`
or for an api you can run the following:
`express first-express-api --no-view`
naviagte to our app `cd first-express-app`
and then run `npm install`
for windows run `SET DEBUG=express-locallibrary-tutorial:* & npm start`
for mac/linux run `DEBUG=express-locallibrary-tutorial:* npm start`
As an example let's run it without the debug statements and see the difference.
next execute `npm install --save-dev nodemon`
This is some sample text to illustrate the
difference between a synchronous and an asychronous file
read in javascript.
\ No newline at end of file
var fs = require("fs");
// Asynchronous read
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
\ No newline at end of file
var fs = require("fs");
var buf = new Buffer(1024);
console.log("Going to open an existing file");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
console.log("Going to read the file");
fs.read(fd, buf, 0, buf.length, 0, function(err, bytes) {
if (err) {
console.log(err);
}
// Print only read bytes to avoid junk.
if(bytes > 0) {
console.log(buf.slice(0, bytes).toString());
}
// Close the opened file.
fs.close(fd, function(err) {
if (err) {
console.log(err);
}
console.log("File closed successfully.");
});
});
});
var http = require('http');
// Options to be used by request
var options = {
host: 'localhost',
port: '8081',
path: '/index.html'
};
// Callback function is used to deal with response
var callback = function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(data) {
body += data;
});
response.on('end', function() {
// Data received completely.
console.log(body);
});
}
// Make a request to the server
var req = http.request(options, callback);
req.end();
\ No newline at end of file
<html>
<head>
<title>Test Page</title>
</head>
<body>
Welcome to 388b winter 2019!!!
</body>
</html>
\ No newline at end of file
var http = require('http');
var fs = require('fs');
var url = require('url');
// Create a server
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
console.log("Pathname is: " + pathname);
// Print the name of the file for which request is made.
console.log("Request for " + pathname + " received.");
// Read the requested file content from file system
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-Type': 'text/html'});
} else {
//Page found
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/html'});
// Write the content of the file to response body
response.write(data.toString());
}
// Send the response body
response.end();
});
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
\ No newline at end of file
const preValues = []
function square(n) {
let result = 0
for (let i= 0; i <= n; i++) {
for (let j=1; j <= n; j++) {
result += 1
}
}
return result
}
function improvedSquare(n) {
if (preValues[n] != null) {
return preValues[n]
}
let result = 0
for (let i= 0; i <= n; i++) {
for (let j=1; j <= n; j++) {
result += 1
}
}
preValues[n] = result
return result
}
//console.log(square(30000))
//console.log(square(30000))
//console.log(square(30000))
//console.log(square(30000))
//console.log(improvedSquare(30000))
//console.log(improvedSquare(30000))
//console.log(improvedSquare(30000))
function fib(n) {
if (n <= 2) {
return 1
} else {
return fib(n-1) + fib(n-2)
}
}
function betterFib(n, prevValues = []) {
if (prevValues[n] != null) {
return prevValues[n]
}
let result
if (n <= 2) {
result = 1
} else {
result = betterFib(n-1, prevValues) + betterFib(n-2, prevValues)
}
prevValues[n] = result
return result
}
console.log(betterFib(50))
console.log(fib(40))
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Exercise 1</title>
</head>
<body>
<script>
"use strict";
/* Your JavaScript here */
</script>
<script src="caching.js"></script>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment