Hello World HTTP Server
Counter
Hello World Module
Parse Log File
Using Forever
Forever commands
Nginx Server Block
Express Framework
NPM
Blogging Test Apps
Bogart Web Framework
Frameworks
Template Engines
http GET and POST requests
for-in loop
Express-Handlebars
Handlebars Helpers
Set Handlebars Views Dir
handlebars layouts
handlebars partial views
sample app
Learning Node.JS Programming
http://nodejs.org/api/http.html
https://github.com/jacwright/date.format
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
https://github.com/request/request
http://nodejs.org/api
https://github.com/cliss/camel/blob/master/camel.js
http://handlebarsjs.com
http://expressjs.com/4x/api.html
https://github.com/ericf/express-handlebars
http://runnable.com/U07z_Y_j9rZk1tTx/handlebars-template-examples-with-express-4-for-node-js
Node.js + Express + Handlebars tutorial
http://codecondo.com/7-minimal-node-js-web-frameworks/
Learning http://nodejs.org/community/
Style Guide http://nodeguide.com/style.html
- http://howtonode.org
- https://github.com/senchalabs/connect
- http://stackoverflow.com/questions/tagged/node.js
- http://jothut.com/cgi-bin/junco.pl/tag/nodejs
- http://jothut.com/cgi-bin/junco.pl/b/17499/NodeJS-for-newbies
- http://nodejs.org
- https://www.npmjs.org
- http://nodejs.org/docs/v0.4.11/api/http.html#http.request
- http://package.json.nodejitsu.com
- http://inessential.com/2013/12/09/getting_started_with_node_js_for_cocoa_
- http://inessential.com/2013/12/09/getting_started_with_web_services_using_
- http://expressjs.com/4x/api.html
- http://howtonode.org/hello-node
- http://www.2ality.com/2011/12/nodejs-shell-scripting.html
- http://www.theprojectspot.com/tutorial-post/Node-js-for-beginners-part-1-hello-world/2
- http://www.theprojectspot.com/tutorial-post/nodejs-for-beginners-callbacks/4
- http://nodeguide.com/beginner.html
- http://blog.modulus.io/absolute-beginners-guide-to-nodejs
Hello World at Command Prompt
Open up a text file and enter:
// Call the console.log function.
console.log("Hello World");
Save file as hello-console.js and then at the command prompt, execute:
node hello-console.js
Obviously, that produces:
Hello World
Hello World HTTP Server
Add the following code to a file called:
hello-server.js
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8080, IP defaults to 127.0.0.1
server.listen(8080);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8080/");
Execute the code with:
node hello-server.js
The following message will be displayed to the console:
Server running at http://127.0.0.1:8080/
Point the browser to the domain/sub domain of the server with the port included. Example:
http://nodejs.soupmode.com:8080
If running the server and the browser on the same machine, use:
http://localhost:8080
If the above server code is running, then the following will be displayed in the browser:
Hello World
To stop this server app, hit Ctrl-C
at the command prompt.
Counter
Add the following code to counter.js
var http = require('http');
var userCount = 0;
http.createServer(function (request, response) {
console.log('New connection');
userCount++;
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello!\n');
response.write('We have had '+userCount+' visits!\n');
response.end();
}).listen(8080);
console.log('Server started');
Execute the code with:
node counter.js
The message displayed to the console will say:
Server started
When accessing the server at port 8080 in the browser, the following will be displayed to the browser:
Hello!
We have had 1 visits!
Back at the server, the console display will say:
New connection
New connection
A refresh on the browser will cause the following message to be sent by the counter.js app:
Hello!
We have had 3 visits!
The counter increments by two.
Explanation from the article :
Note: You might see the counter going up by two for each request, this is because your browser is requesting the favicon from the server (http://localhost:8080/favicon.ico).
You should also see that Node.js is logging each request it receives to the console.
The main thing we're doing here is setting a 'userCount' variable and incrementing on each request. We're then writing the 'userCount' in the response text.
Hello World Module
http://nodeguide.com/beginner.html
Create the file main.js
with the following code:
var hello = require('./hello');
hello.world();
In the same directory, create another file called hello.js
with the following code:
exports.world = function() {
console.log('Hello World');
}
Execute at the command prompt:
node main.js
The console will display the following:
Hello World
Parse Log File
http://blog.modulus.io/absolute-beginners-guide-to-nodejs
Using Forever
Running a Node.js app as daemon.
The purpose of Forever is to keep a child process (such as your node.js web server) running continuously and automatically restart it when it exits unexpectedly.
I made a change to my Ghost blog installation config file. I killed the Ghost index.js process. When I did, it automatically restarted and loaded the new config settings.
http://stackoverflow.com/questions/4018154/node-js-as-a-background-service
https://www.npmjs.org/package/forever
http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever
https://github.com/nodejitsu/forever
$ npm install forever
$ forever start /home/nodejs/counter.js
Forever commands
usage: forever [start | stop | stopall | list] [options] SCRIPT [script options]
options:
start start SCRIPT as a daemon
stop stop the daemon SCRIPT
stopall stop all running forever scripts
list list all running forever scripts
Nginx Server Block
$ cd /etc/nginx/sites-available
$ vim nodejs.yoursite.com
The server block file:
server {
listen 80;
server_name nodejs.yoursite.com;
location / {
proxy_pass http://nodejs.yoursite.com:8080;
}
}
$ service restart nginx
In browser, access: http://nodejs.yoursite.com
Each refresh will update the counter. Should see something like this:
Hello!
We have had 18 visits!
Express Framework
http://blog.modulus.io/absolute-beginners-guide-to-nodejs
npm install express
When you install a module, it will put it in a node_modules folder inside your application directory. You can now require it like any built-in module.
file: static-file-server.js
code:
var express = require('express'),
app = express();
app.use(express.static(__dirname + '/public'));
app.listen(8081);
execute: node static-file-server.js
NPM
There are thousands of modules available that solve almost all typical problems that you're likely to encounter. Remember to check npm before re-inventing the wheel. It's not unheard of for a typical Node.js application to have dozens of dependencies.
In the previous example we manually installed Express. If you have a lot of dependencies, that's not going to be a very good way to install them. That's why npm makes use of a package.json file.
package.json
{
"name" : "MyStaticServer",
"version" : "0.0.1",
"dependencies" : {
"express" : "3.3.x"
}
}
A package.json file contains an overview of your application. There are a lot of available fields, but this is pretty much the minimum. The dependencies section describes the name and version of the modules you'd like to install. In this case I'll accept any version of Express 3.3. You can list as many dependencies as you want in this section.
Now instead of installing each dependency separately, we can run a single command and install all of them.
$ npm install
When you run this command npm will look in the current folder for a package.json file. If it finds one, it will install every dependency listed.
Blogging Test Apps
https://github.com/creationix/wheat - Wheat is a blogging engine that reads a git repo full of markdown articles and presents them as a website.
http://howtonode.org/bogart-couchdb - A Simple Blog with CouchDB, Bogart, and Node.js
https://github.com/cliss/camel
Bogart Web Framework
https://github.com/nrstott/bogart
Frameworks
- http://nodeframework.com
- http://blog.newrelic.com/2014/08/15/node-js-frameworks-hapi-js-restify-geddy
Template Engines
Handlebars is one popular template engine.
Dust.js
https://nodejsmodules.org/tags/template
http GET and POST requests
http://stackoverflow.com/questions/6968448/where-is-body-in-a-nodejs-http-get-response
http://stackoverflow.com/questions/4935632/parse-json-in-javascript
for-in loop
http://stackoverflow.com/questions/684672/loop-through-javascript-object
Express-Handlebars
Separate from Handlebars for Express.
https://www.npmjs.org/package/express3-handlebars
THIS PACKAGE HAS BEEN RENAMED TO: express-handlebars A Handlebars view engine for Express which doesn't suck
Handlebars Helpers
http://stackoverflow.com/questions/18273705/how-to-loop-through-array-inside-an-array-in-handlebar-with-node-js-express
http://theenlighteneddeveloper.com/2014/04/11/simple-application-running-on-node-js-with-handlebars-express-and-mongoose/
Set Handlebars Views Dir
By default, it will look in a folder called views from the directory the script is. If you use a different dir you must specify it.
app.set('views', __dirname + '/views');
handlebars layouts
https://github.com/shannonmoeller/handlebars-layouts
handlebars partial views
http://stackoverflow.com/questions/16385173/node-js-express-handlebars-js-partial-views
https://github.com/donpark/hbs/tree/master/examples/partial
http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers
sample app
Node.js with Handlebars - Express - Mongoose - MongoDB
http://theenlighteneddeveloper.com/2014/04/11/simple-application-running-on-node-js-with-handlebars-express-and-mongoose/
https://github.com/xelita/mongoose-handlebars-sample
#nodejs - #programming - #javascript - #howto
From JR's : articles
1042 words - 10105 chars
- 5 min read
created on
updated on
- #
source
- versions
- backlinks
Related articles
React Native - Nov 01, 2016
NodeJS for newbies - Nov 01, 2013
App idea to learn new code - Mar 10, 2014
Links feb 25, 2017 - Feb 25, 2017
How to enable and disable JavaScript in Firefox - Apr 23, 2016
more >>