码迷,mamicode.com
首页 > Windows程序 > 详细

Setup node.js on Windows Server

时间:2015-06-05 19:37:56      阅读:642      评论:0      收藏:0      [点我收藏+]

标签:

1. 环境:windows server 2012 (64bit)

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>

  

 

Setup node.js on Windows Server

标签:

原文地址:http://www.cnblogs.com/binglong/p/4555407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!