Setup node. js on Windows Server

Source: Internet
Author: User
Tags emit sendfile

1. Environment: Windows Server (64bit)

2. Download: Download Windows Installer (. msi) from https://nodejs.org/download/

3. Install: Next ....

4. Test:

4.1 Writing examples

var http = require (' http '); Http.createserver (function (req, res) {     Res.writehead ($, {' Content-type ': ' Text/plain '});     

4.2 Execution

4.3 Enter ...

Access http://127.0.0.1:1337 via browser to get Hello World response

Play Socket.io

1. Follow it: http://socket.io/get-started/chat/

2. Problems encountered

2.1 Install Express when you encounter a proxy problem

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 is behind a proxy, please make sure that thenpm err! ' Proxy ' config is set properly.  See: "npm help config"

  

2.1.1 Find Proxy

Http://superuser.com/questions/346372/how-do-i-know-what-proxy-server-im-using

2.1.2 Setting Proxy

Call [npm Help Config] know ...

2.2 Question two:

PS e:\test> node index.jslistening on *:3000express deprecated res.sendfile:Use Res.sendfile instead index.js:6:7

2.2.1 Changed sendfile to Sendfile

Problems encountered:

Typeerror:path must be absolute or specify ROOT to Res.sendfile

Follow it: Http://stackoverflow.com/questions/25463423/res-sendfile-absolute-path

I change:

App.get ('/', function (req, res) {  res.sendfile (path.join (__dirname, ' index.html '));});

There's still a problem.

Referenceerror:path is not defined "

Follow it: Https://groups.google.com/forum/#!topic/express-js/4FGHcV9xGwQ

I'll change it again:

var path = require (' path '); App.get ('/', function (req, res) {  res.sendfile (path.join (__dirname, ' index.html '));});

Final version

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 (The function () {  console.log (' listening on *:3000 ');});

Index.html

<!doctype html>

  

Setup node. js on Windows Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.