Created
August 3, 2015 18:27
-
-
Save mayfer/9cf22fc87e8da1bb97bd to your computer and use it in GitHub Desktop.
Node.js
This file contains hidden or 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
// now using express to handle routing | |
var express = require('express'); | |
var app = express(); | |
var http = require('http'); | |
var path = require('path'); | |
// attach a middleware | |
var server = http.Server(app); | |
app.set("view engine", "ejs"); | |
app.get('/', function(req, res){ | |
res.render("index.ejs", { message: "yooooo", }); | |
}); | |
app.get('/murat', function(req, res){ | |
res.send(" yes yes we get it your name is murat"); | |
}); | |
server.listen(8888, function(){ | |
console.log('listening on *:8888'); | |
}); | |
app.use("/static", express.static(path.join(__dirname, 'public'))); |
This file contains hidden or 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
<!doctype> | |
<html> | |
<head> | |
<title>hi</title> | |
<style> | |
body, html { | |
background: #aaa; | |
font-family: monospace; | |
} | |
</style> | |
</head> | |
<body> | |
<% for(var i=0; i<10; i++) { %> | |
hello <%= message %> <%= i+1 %> <br /> | |
<% } %> | |
</body> | |
</html> |
This file contains hidden or 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'); | |
var makeRequest = function(url, callback) { | |
var callback_inner = function(response) { | |
var str = ''; | |
//another chunk of data has been recieved, so append it to `str` | |
response.on('data', function (chunk) { | |
str += chunk; | |
}); | |
//the whole response has been recieved, so we just print it out here | |
response.on('end', function () { | |
callback(str); | |
}); | |
} | |
var options = { | |
host: url, | |
path: '/', | |
}; | |
console.log("before request"); | |
http.request(options, callback_inner).end(); | |
console.log("after request"); | |
} | |
var tellMyFortune = function() { | |
return "you have an unfortunate fortune"; | |
} | |
module.exports = { | |
request: makeRequest, | |
fortune: tellMyFortune, | |
} | |
This file contains hidden or 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
// running this file will start a basic web server on port 8888 | |
var http = require("http"); | |
var fetch = require("./request2.js"); | |
var fs = require('fs'); | |
var handler = function(request, response) { | |
if(request.url == "/") { | |
console.log("starting request"); | |
fetch.request("www.google.ca", function(data) { | |
console.log("got contents"); | |
response.write(data); | |
response.end(); | |
}); | |
console.log("request made, moving on..."); | |
} else if(request.url == "/script.js") { | |
fs.readFile("./script.js", function(err, contents) { | |
response.write(contents); | |
response.end(); | |
}); | |
} else { | |
// 404 | |
response.writeHead(404); | |
response.write(request.url + " NOT FOUND"); | |
response.end(); | |
} | |
} | |
var server = http.createServer(handler); | |
var port = 8888; | |
server.listen(port); | |
console.log("listening on " + port); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment