一直对NodeJs有很大的兴趣~趁着今天公司里的事不多趁机学习下~特地带来了笔记本(笔记本里装了Ubuntu),NodeJs是基于V8Js引擎的服务器端的Js环境,算是一个中间件,底层还是由C/C++实现的。以前对于前后端的数据验证神木的都要做两层,即在Js端要做一层数据交互验证,而在服务端由于用户可以禁用JS,所以服务端必须额外的对数据进行一次验证,效率阿。。。。。有了NodeJs这一切便可以只执行一次就可以了,这是它吸引我的起始理由~~而后来我发现它更加的牛逼~~对于长链接(keep alive)型的请求处理等等都是让人有耳目一新的感觉~
还是先装了它吧~win下装node比较尴尬,还是ubuntu下的感觉比较好~直接说ubuntu下的安装。
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core
需要装apache大工具包~还有git,装过git的可以省去下面那段代码。
接下去就是安装了。
git clone git://github.com/joyent/node.git
cd node
./configure
make
sudo make install
这下就把node安在了自己的Ubuntu里面了~
接下来来个测试页面~
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
将上面的代码保存为hello.js放到Ubuntu用户文件夹里。运行下列代码:
node hello.js
终端显示Server running at http://127.0.0.1:8124/
打开浏览器就能见到效果了。还不错吧~~只用js就能写服务端了~
接下来来个大点的东西,一个单页面的实时聊天系统。是官方提供的demo,效果真的很好~(https://github.com/ry/node_chat)
用终端进入文件夹后node server.js就行了~
效果图:
完全实时的进行显示~~接下来会慢慢的认识下node~~试着自己写个demo~
附上最近的windows下的安装步骤:(2011.8.24)
Step 1. 下载
node.js在windows下是要安装在Cygwin下的,去Cygwin网站下载Cygwin安装程序。
Cygwin网站:http://cygwin.com/
直接下载地址:http://cygwin.com/setup.exe
Step 2. 安装
安装你下下来的Cygwin程序,基本上只需下一步就可以。
在Choose A Download Source界面选择-》install from internet
在Select Your Internet Connection界面选择-》Direct Connect
在Choose A Download Site界面选择-》这个自己选择了,就是选择下载的镜像,推荐使用.jp结尾的日本网站,速度比较快
注意:如果你是首次安装,可能会弹出警告提示,不管管他,点击ok就可以。
Step 3. 选择安装组件
注意:由于组件较多,建议使用搜索的方式来查找。点击名字即可选中,选中后后面会有一个小方块,里面有X(叉)表示选中。
Devel 分类
gcc-g++: C++ compiler
gcc-mingw-g++: Mingw32 support headers and libraries for GCC C++
gcc4-g++: G++ subpackage
git: Fast Version Control System – core files
make: The GNU version of the 'make' utility
openssl-devel: The OpenSSL development environment
pkg-config: A utility used to retrieve information about installed libraries
zlib-devel: The zlib compression/decompression library (development)
Editor 分类
vim: Vi IMproved - enhanced vi editor
Python 分类
全部
注意:全部安裝,你只要点选 Python 右边的图标切换至 Install 即可。
Web 分类
wget: Utility to retrieve files from the WWW via HTTP and FTP
curl: Multi-protocol file transfer command-line tool
下一步之后会提示你有许多相依的组件需要安装,基本上直接按下一步即可。
注意:由于组件都是在线下载,可能有点漫长,等待即可。
Step 4. 运行ash.exe
安装完后会在桌面生成图标(如果你选了在桌面生成图标项的话),注意不是运行这个图标!
打开文件夹,进入C:\cygwin\bin\ 目录中,这个是默认的安装目录,如果你修改过安装目录,自己进入到cygwin的安装目录下的bin目录中。
运行ash.exe
在弹出的命令窗口中执行 ./rebaseall -v 命令
等命令执行完成执行 exit 退出 ash
Step 5. 下载并安装Node.js
运行桌面上的Cygwin程序。
在命令窗口中输入 wget http://nodejs.org/dist/node-v0.4.7.tar.gz 回车。
注意:我在写这篇文章是node.js的最新版本是node-v0.4.7, http://nodejs.org/dist/node-v0.4.7.tar.gz 是node.js的下载地址,你在看这篇文章时可以自己去node.js的网站获取最新的下载路径替换就可以了
Node.js网站:http://nodejs.org/
在命令窗口中输入 tar xf node-v0.4.7.tar.gz 回车,node-v0.4.7.tar.gz为你自己下载的版本。
在命令窗口中输入 cd node-v0.4.7 回车。
在命令窗口中输入 ./configure 回车。
在命令窗口中输入 make 回车。
在命令窗口中输入 make install 回车。
ok,到这里你已经安装完成node.js了
在命令窗口中输入 node --version 回车,看看你的node.js版本。
Step 6. 设置DNS
打开文件夹,进入到C:\cygwin\etc\ 文件夹下。
手动建立 resolv.conf 文件。
用记事本打开刚建立的resolv.conf文件,添加如下
nameserver 8.8.8.8
nameserver 8.8.4.4
保存并关闭。
Step End. 测试Node.js是否可以运行
到此你就已经搭建完成了,但是不知道是否可以正确运行,那么,下面让我们来写个示例文件来测试下。
在C:\cygwin\ 目录下建立 example.js
输入
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('Hello World');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
保存(注意:保存为UTF-8格式,要不然会报语法错误)。
运行cygwin,在命令窗口中输入 node /example.js 回车
是否看见了Server running at http://127.0.0.1:8888/
不要关闭cygwin,打开浏览器,访问http://127.0.0.1:8888/ 看看是否出现了hello world的页面。
下面一些node的资料~写一下备用~
国内
- NodeJS专业中文社区: http://cnodejs.org
- 官方API文档: http://cnodejs.org/api/
- 最全中文入门: http://cnodejs.org/blog/?p=32
- NodeJS中文翻译: http://wiki.grati.org/index.php?title=NodeJS%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3
- 又一篇中文入门: http://news.csdn.net/a/20100312/217428.html
- 一个靠谱的nodejs学习笔记: http://blog.csdn.net/zhangxin09/category/726487.aspx?PageNumber=2
- 用node写MVC框架:http://www.cnblogs.com/QLeelulu/archive/2011/01/28/nodejs_into_and_n2mvc.html
国外
- 官方网站: http://nodejs.org/
- 英文文档: http://nodejs.org/docs/v0.3.5/api/all.html
- node包管理器: http://npmjs.org/
- 官方介绍: http://nodejs.org/jsconf2010.pdf
- Node的模块大全,必阅读: https://github.com/ry/node/wiki/modules
- 一个Node入门网站: http://howtonode.org
- 一个不错的Node博客: http://blog.nodejitsu.com/
- 包含了不少node信息的前端blog: http://dailyjs.com/
- 一个48小时NodeJS的竞赛网站: http://nodeknockout.com/
- 号称世界上第一个用node搭建的SaaS应用: http://transloadit.com/
- 如何写Node.JS的插件: https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/
- 打造在线监控系统: http://blog.new-bamboo.co.uk/2009/12/7/real-time-online-activity-monitor-example-with-node-js-and-websocket
- 搭建一个webservice: http://blog.nodejitsu.com/a-simple-webservice-in-nodejs
基于node的项目和应用程序
- node_chat by ry
- q-chess by wsdookadr — chess game multiplayer server using faye + client written using Qooxdoo
- node3p by ncb000gt — AmazonMP3 downloader
- node3p-web by ncb000gt — Web interface for node3p
- nodelint by tav — run jslint from the command line
- minlint – minimalist version of jslint from command line
- nodeload by benschmaus and jonjlee — utility for benchmarking HTTP APIs
- nodewiki — a simple wiki based on node and redis
- blog.js — Simple blogging engine based on node.js
- cassiterite — Another blogging engine based on node.js + Tokyo Tyrant + nginx
- Sousaball by creationix (sousaball.creationix.com)
- DinMaker by creationix (live demo at chat.creationix.com)
- Flickr Spy — Discover where your Flickr contacts are commenting
- azathoth — simple HTTP traffic/load generator and benchmark proof of concept
- antinode — Simple static file HTTP Server with logging
- NodeMachine — Port of WebMachine for Node.js
- LifeInText — LiT is a mashup platform for personal projects and snippets.
- Ajax IM — Ajax IM is an instant messaging server and framework for the browser.
- 7 Minutes Chrono – A speed-dating chat application. Uses websockets and node.js
- Persevere – REST/HTTP + JSON-based web data project that includes Pintura, Perstore, CommonJS-Utils
- js.io — Javascript Networking Library for building real-time web applications
- BusBound – iPhone web application built with express framework
- JSONPify – a JSONP proxy for client applications
- Djangode – Django’s regex-based URL handling, utility functions, and html templating for node
- The WPilot Project – A multiplayer, space shooter, action game in your browser. Server is built in node
- Socket.IO – An abstraction over all the realtime transports (WebSocket, long polling, XHR multipart, etc) for the Node HTTP server
- zztmmo – a zzt based massive multiplayer online game built in node.js and jQuery (currently only single player)
- diretto – Distributed Recon/Reporting Transceiver Toolkit – Uses Node.js for web-based media storage and for its RESTful webservice implementation
- ElusiveHippo – Push deployment for nodejs (non-functional / doesn’t work)
- Chess Riot – a live Lightning Chess app built in node.js, express, CoffeeScript and jQuery. By @weepy
- Realie – Real-time Collaborative Editor using Web Sockets, Node.js & Redis (non-functional / doesn’t work)
- GrowlHub – Growl Notifications for GitHub
- Simple Node.js MUD – Simple multi-user dungeon using Node.js
- wtfjs – what the fuck javascript!
- vertex.js – A graph database inspired by filesystems built on node.js and tokyocabinet.
- The API Playground – A website where journalists can play with API examples. (CoffeeScript/Express) apiplayground.org
- Szpil – CRM using Node and TokyoCabinet. Shared client/server database and model code, user interface decoupled from the network, APIretrieval and authorization performed at the meta-data layer using set union and intersection on indexes to avoid JSONdeserialization/serialization overhead.
- vows.js – “Asynchronous behaviour driven development for Node.”
- jimi — A framework for writing modular web applications in node.js (tutorial)
- stackvm — Virtual machines on the web!
- paperserve — Quick and dirty web server from your working directory
- Trending Topics Client — Command line tool to get trending topics from Twitter
- nTunes by TooTallNate — A REST (HTTP) API for interacting with iTunes written in NodeJS.
- nTunesAnywhere by TooTallNate — A mobile WebApp to access your iTunes library from anywhere (using nTunes and jQTouch)!
- Node.js Interactive Shell — Interactive shell for Node.js
- Alligator — Application Server on top of NodeJS (JSSP and SSJS support)
- node-build — ant like build tool for nodejs without the xml hassle
- scriptable-node-irc-bot — Scriptable Node IRC Bot
- Top Twitter Trends — Real time trending tweets by node.js, nginx and mongodb
- Cloud9 — Cloud9 IDE is aiming to be the IDE for Javascript developers, built with NodeJS and lots of other Node modules
- OPOWER Jobs — Open source example of a full live site using Express, Connect, EJS, AssetManager, Logging, and other Node modules.
- nodemon — Development tool for automatically restarting Node server when changes are made to any app files
- jsbuild — A command-line utility that provides building CommonJS modules and packages for web
- NodeGame Shooter – Multiplayer asteroids with powerups etc. Using a fat Node.js server with thin clients
- NodeCli – A super-simple class for Node.js to spice up your Node.js console output. Position cursor, clear screen, change colors
- noatta.ch — Instant Personal File Sharing
- node-tracker — BitTorrent tracker implemented with Node.js
- Scrabbly – A Massively Multiplayer word game written on node.js. A Node Knockout winner.
- node-boilerplate — Everything you need to get started on a Node.js website with express and socket.IO
- Orona — A multiplayer game of tank warfare. (Remake of the classic Mac game Bolo.)
- Hummingbird — A realtime web analytics app using socket.io
- Node.io — A distributed data scraping and processing framework
- Backbone Examples Ported From Knockout — by Mario Gutierrez
- TxtAtlas: Put your texts on the map ://txtatlas.com — A demo Twilio/Node.js/Redis application by Stephen J. Walters
- Substance — A data-driven and cloud-aware realtime document authoring engine
- Graffiti β — Interactive multi-user shared canvas whiteboard
- BirdChat — Chat with your friends from Twitter
- bookfriend.me — Community site for Barnes and Noble Nook owners who want to borrow/lend books
- Fishl.com — Mark and save quotes you like all over the Web
- slyncr — bookmarklet to control web slideshows via a remote site
- Applifier – Largest social game cross promo network)(reference http://twitter.com/jussil/status/12048190433918976)
- memcached.js — Port of Memcached server for Node.js
- Loggly — Webservice for receiving syslog and HTTP based logs.
- spotted@ — spotted @ provides local classifieds for jobs, housing, for sale, personals, services, local community, and events.
- designo — Realtime Twitter client demo exploiting Node.js+Websockets+HTML5
- Bittorrent Tracker – High performance bittorrent tracker by anhel
- The-M-Project – HTML5 JavaScript framework for mobile apps with nodeJS build tool