Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created December 8, 2014 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mayfer/d94bb1cbc7dfeadf7007 to your computer and use it in GitHub Desktop.
Save mayfer/d94bb1cbc7dfeadf7007 to your computer and use it in GitHub Desktop.
node examples, step by step.
// run this with "node 0.js"
console.log("Hello World");
// running this file will start a basic web server on port 8888
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {
"Content-Type": "text/plain",
});
response.write("Hello Universe");
response.end();
}).listen(8888);
var http = require("http");
var url = require("url");
function onRequest(request, response) {
// now we parse the url path to serve content based on the url
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
if(pathname != '/') {
response.write("\n");
response.write("requested path "+pathname);
}
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
var http = require("http");
var url = require("url");
var fs = require("fs");
function start() {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
response.writeHead(200, {"Content-Type": "text/html"});
if(pathname == "/index.html") {
// serve content from a static html file on disk
var html = fs.readFileSync("index.html");
response.write(html);
}
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
start();
var http = require("http");
var url = require("url");
var fs = require("fs");
// load a js library from the current folder
var snake = require("./script");
function start() {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
if(pathname == "/script.js") {
// serve static js file with the correct content type header
response.writeHead(200, {"Content-Type": "application/javascript"});
var js = fs.readFileSync("script.js");
response.write(js);
}
else if(pathname == "/index.html") {
response.writeHead(200, {"Content-Type": "text/html"});
var html = fs.readFileSync("index.html");
response.write(html);
} else {
// if no route match is found, return 404 not found.
response.writeHead(404, {"Content-Type": "text/html"});
response.write("not found");
}
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
start();
// now using express to handle routing
var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
// showing a basic library example. view contents of some_lib.js to see how exports is used.
var new_lib = require('./some_lib.js');
console.log("Imported from some_lib.js:", new_lib);
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'index.html'));
});
http.listen(8888, function(){
console.log('listening on *:8888');
});
app.use(express.static(__dirname));
var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
// tell express to use EJS templating
app.set('view engine', 'ejs');
app.get('/', function(req, res){
// set some data variables to use within the template
data = {
message: "hello werld",
}
res.render(path.join(__dirname, 'index.ejs'), data);
});
http.listen(8888, function(){
console.log('listening on *:8888');
});
app.use(express.static(__dirname));
<!doctype>
<html>
<head>
<title>hi</title>
<script src="script.js"></script>
<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>
<!doctype>
<html>
<head>
<title>hi</title>
<script src="script.js"></script>
<style>
body, html {
background: #aaa;
font-family: monospace;
}
</style>
</head>
<body>
hello browser
</body>
</html>
(function(snake){
snake.settings = {
height: 400,
width: 600,
}
snake.grid = {
size: 15,
}
snake.snake = null;
snake.apples_list = [];
snake.apples = {
};
snake.add_apple = function(public_id, x, y) {
if(x == undefined || y == undefined) {
x = 10;
y = 10;
}
snake.apples[public_id] = {
x: x,
y: y,
}
snake.apples_list.push(public_id);
}
snake.remove_apple = function(public_id) {
delete snake.apples[public_id];
var i = snake.apples_list.indexOf(public_id);
if(i != -1) {
snake.apples_list.splice(i, 1);
}
}
})(typeof snake === 'undefined'? this['snake']={}: snake);
var a_value = 100;
exports.b_value = a_value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment