当前位置:首页 > Windows程序 > 正文

Setup node.js on Windows Server

2021-05-26 Windows程序

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 :1337/‘);

  4.2 执行

node example.js

  4.3 进入。。。

通过浏览器访问:1337得到Hello World的响应

玩耍 socket.io 

1. follow 它: 

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

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 它: 

我改:

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></ul> <form action=""> <input autocomplete="off" /><button>Send</button> </form> <script src="http://www.mamicode.com/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

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/71247.html