Created
April 22, 2013 13:02
-
-
Save jalalhejazi/5434727 to your computer and use it in GitHub Desktop.
Node: Create Simple NodeJS server reading a file from fileSystem
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 fs = require('fs'); | |
http.createServer(function(request, response) { | |
response.writeHead(200, { | |
'Content-Type': 'text/html' | |
}); | |
fs.readFile('index.html', function(err, content){ | |
response.write(content); | |
response.end(); | |
}); | |
}).listen(1111); | |
console.log("Listening on port 1111"); |
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 lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Bootstrap, from Twitter</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta name="description" content=""> | |
<meta name="author" content=""> | |
<!-- Le styles --> | |
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet"> | |
<style type="text/css"> | |
body { padding-top: 60px; padding-bottom: 40px; } | |
</style> | |
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css" rel="stylesheet"> | |
<!-- Le fav and touch icons --> | |
<link rel="shortcut icon" href="http://twitter.github.com/bootstrap/assets/images/favicon.ico"> | |
<link rel="apple-touch-icon" href="images/apple-touch-icon.png"> | |
<link rel="apple-touch-icon" sizes="72x72" href="http://twitter.github.com/bootstrap/assets/images/apple-touch-icon-72x72.png"> | |
<link rel="apple-touch-icon" sizes="114x114" href="http://twitter.github.com/bootstrap/assets/images/apple-touch-icon-114x114.png"> | |
</head> | |
<body> | |
<div class="navbar navbar-fixed-top"> | |
<div class="navbar-inner"> | |
<div class="container"> | |
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</a> | |
<a class="brand" href="#">NodeJS</a> | |
<div class="nav-collapse"> | |
<ul class="nav"> | |
<li class="active"><a href="#">app.js</a></li> | |
<li hidden ><a href="#about">About</a></li> | |
<li hidden ><a href="#contact">Contact</a></li> | |
</ul> | |
</div><!--/.nav-collapse --> | |
</div> | |
</div> | |
</div> | |
<div class="container"> | |
<!-- Main hero unit for a primary marketing message or call to action --> | |
<div class="hero-unit"> | |
<h1>Hello, from NodeJS Server </h1> | |
<pre>var http = require('http'); | |
var fs = require('fs'); | |
http.createServer(function(request, response) { | |
response.writeHead(200, { | |
'Content-Type': 'text/html' | |
}); | |
fs.readFile('index.html', function(err, content){ | |
response.write(content); | |
response.end(); | |
}); | |
}).listen(1111); | |
console.log("Listening on port 1111"); | |
</pre> | |
<p><a class="btn btn-primary btn-large">Learn more »</a></p> | |
</div> | |
<!-- Example row of columns --> | |
<div class="row"> | |
<div class="span8"> | |
<h2>NodeJS support for Access-Control-Allow-Origin</h2> | |
<pre> | |
// Support CORS in NodeJS using Express module: | |
setHeaders = function (req,res,next) { | |
res.header("X-Powered-By","nodejs"); | |
// if ajax set access control | |
if (req.xhr) | |
{ | |
res.header("Access-Control-Allow-Origin", req.header('origin')); | |
res.header("Access-Control-Allow-Headers", "X-Requested-With"); | |
} | |
next(); | |
}; | |
// then apply that function to all routes via | |
app.use(setHeaders); | |
// (usually within app.configure()) | |
//*********************************// | |
app.all('/posts', function(req, res){ | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "X-Requested-With"); | |
res.send( | |
{ posts : '...' } | |
); | |
}); | |
</pre> | |
</div> | |
</div> | |
<hr> | |
<footer> | |
<p>© NodeJS</p> | |
</footer> | |
</div> <!-- /container --> | |
<!-- Le javascript | |
================================================== --> | |
<!-- Placed at the end of the document so the pages load faster --> | |
<script src="http://code.jquery.com/jquery.min.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-transition.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-alert.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-modal.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-dropdown.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-scrollspy.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-tab.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-tooltip.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-popover.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-button.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-collapse.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-carousel.js"></script> | |
<script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-typeahead.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment