Setup node.js on Windows Server
2015-06-05 18:57
645 查看
1. 环境:windows server 2012 (64bit)
2. 下载: 从 https://nodejs.org/download/ 下载 Windows Installer (.msi)
3. 安装:下一步。。。。
4. 测试:
4.1 写范例
4.2 执行
4.3 进入。。。
通过浏览器访问http://127.0.0.1:1337得到Hello World的响应
玩耍 socket.io
1. follow 它: http://socket.io/get-started/chat/
2. 所遇问题
2.1 安装express 时遇到 proxy 问题
2.1.1 找proxy
http://superuser.com/questions/346372/how-do-i-know-what-proxy-server-im-using
2.1.2 设置proxy
call [npm help config] 就知道啦。。。
2.2 问题二:
2.2.1 改sendfile为sendFile
遇到问题:
follow 它: http://stackoverflow.com/questions/25463423/res-sendfile-absolute-path
我改:
还有问题
follow 它:https://groups.google.com/forum/#!topic/express-js/4FGHcV9xGwQ
我再改:
最终版本
index.js
index.html
2. 下载: 从 https://nodejs.org/download/ 下载 Windows Installer (.msi)
3. 安装:下一步。。。。
4. 测试:
4.1 写范例
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/');
4.2 执行
node example.js
4.3 进入。。。
通过浏览器访问http://127.0.0.1:1337得到Hello World的响应
玩耍 socket.io
1. follow 它: http://socket.io/get-started/chat/
2. 所遇问题
2.1 安装express 时遇到 proxy 问题
npm ERR! node v0.12.4 npm ERR! npm v2.10.1 npm ERR! code ECONNREFUSED npm ERR! errno ECONNREFUSED npm ERR! syscall connect npm ERR! Error: connect ECONNREFUSED npm ERR! at exports._errnoException (util.js:746:11) npm ERR! at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19) npm ERR! { [Error: connect ECONNREFUSED] npm ERR! code: 'ECONNREFUSED', npm ERR! errno: 'ECONNREFUSED', npm ERR! syscall: 'connect', npm ERR! parent: 'oven' } npm ERR! npm ERR! If you are behind a proxy, please make sure that the npm ERR! 'proxy' config is set properly. See: 'npm help config'
2.1.1 找proxy
http://superuser.com/questions/346372/how-do-i-know-what-proxy-server-im-using
2.1.2 设置proxy
call [npm help config] 就知道啦。。。
2.2 问题二:
PS E:\test> node index.js listening on *:3000 express deprecated res.sendfile: Use res.sendFile instead index.js:6:7
2.2.1 改sendfile为sendFile
遇到问题:
TypeError: path must be absolute or specify root to res.sendFile
follow 它: http://stackoverflow.com/questions/25463423/res-sendfile-absolute-path
我改:
app.get('/', function(req, res){ res.sendFile(path.join(__dirname,'index.html')); });
还有问题
ReferenceError: path is not defined"
follow 它:https://groups.google.com/forum/#!topic/express-js/4FGHcV9xGwQ
我再改:
var path = require('path');
app.get('/', function(req, res){ res.sendFile(path.join(__dirname,'index.html')); });
最终版本
index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
app.get('/', function(req, res){ res.sendFile(path.join(__dirname,'index.html')); });
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
io.emit('chat message', msg);
console.log('message: ' + msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
index.html
<!doctype html> <html> <head> <title>Socket.IO chat</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font: 13px Helvetica, Arial; } form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } #messages { list-style-type: none; margin: 0; padding: 0; } #messages li { padding: 5px 10px; } #messages li:nth-child(odd) { background: #eee; } </style> </head> <body> <ul id="messages"></ul> <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script> var socket = io(); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); </script> </body> </html>
相关文章推荐
- 5.1.10 Populating Next Right Pointers in Each Node II
- node-glob学习
- 用NODEJS处理EXCEL文件导入导出,文件上传
- iOS 用GDataXMLNode创建和解析XML
- LeetCode_19---Remove Nth Node From End of List
- 【NodeJS】---express配置ejs mongoose route等
- nodejs安装及环境配置
- leetcode 19 -- Remove Nth Node From End of List
- nodePPT 这可能是迄今为止最好的网页版PPT
- NodeJs安装教程
- 用 node 实现仿微信风格的 token 获取接口
- Node.js 学习资源总结
- Use Node.js DDP Client on Arduino Yun to Access Meteor Server
- Reverse Nodes in k-Group
- nodejs(npm)——创建全局链接
- 一个强大的UI node 抽象
- nodejs——require单次加载
- nodejs创建包(建立nodejs工程)
- nodejs exports和module.exports区别
- Reverse Nodes in k-Group