Setup node.js on Windows Server

來源:互聯網
上載者:User

標籤:

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.4npm ERR! npm  v2.10.1npm ERR! code ECONNREFUSEDnpm ERR! errno ECONNREFUSEDnpm ERR! syscall connectnpm ERR! Error: connect ECONNREFUSEDnpm 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 thenpm 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.jslistening on *:3000express 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.