Created
July 6, 2015 18:34
-
-
Save mayfer/cba0146a2399d0708edb to your computer and use it in GitHub Desktop.
Intro to Node!
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
// run this with "node hello_world.js" | |
console.log("Hello World", __dirname); |
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> | |
<html> | |
<head> | |
<script src="../script.js"></script> | |
<title>hi</title> | |
<style> | |
body, html { | |
background: #aaa; | |
font-family: monospace; | |
} | |
</style> | |
</head> | |
<body> | |
hello browser | |
</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"); | |
} | |
module.exports = { | |
makeRequest: makeRequest, | |
} | |
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').Server(app); | |
var path = require('path'); | |
var request = require('./request2'); | |
app.get('/', function(req, res){ | |
res.sendFile(path.join(__dirname, 'index.html')); | |
}); | |
app.get('/reddit', function(req, res){ | |
var responseHandler = function(response){ | |
res.send(response); | |
} | |
request.makeRequest('www.reddit.com', responseHandler); | |
}); | |
http.listen(8888, function(){ | |
console.log('listening on *:8888'); | |
}); | |
app.use(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
//Lets require/import the HTTP module | |
var http = require('http'); | |
var fs = require('fs'); | |
//Lets define a port we want to listen to | |
const PORT = 8080; | |
//We need a function which handles requests and send response | |
function handleRequest(request, response){ | |
if(request.url == "/") { | |
fs.readFile("./index.html", function(err, html) { | |
response.setHeader("Content-Type", "text/html"); | |
response.end(html); | |
}); | |
} else { | |
response.statusCode = 404; | |
response.end('not found!!'); | |
} | |
} | |
//Create a server | |
var server = http.createServer(handleRequest); | |
//Lets start our server | |
server.listen(PORT, function(){ | |
//Callback triggered when server is successfully listening. Hurray! | |
console.log("Server listening on: http://localhost:%s", PORT); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment