简体   繁体   中英

Basic idea behind node.js implementation?

Here's my current theory as to how node.js will work:

  1. I install node.js on my server so that it can interpret javascript files.
  2. I then I write my javascript files and put them on my server (just as I would with PHP files).
  3. I then interact with these files via my client-side javascript.

Obviously I've got something wrong, because I've been looking for a tutorial for the past hour or so which will teach me how to install it on my server - but they all seem to be focused on installing it locally.

Could someone give a dot-point rundown of how the final implementation will work?

You install it on a "server" as you would for any other machine -- with admin/root access via an installer or package manager .

Now, this assumes, by "server," you're referring to a computer. If, instead, you mean an existing "server application," such as Apache or IIS -- Node.js doesn't integrate directly with these. It primarily replaces them, allowing you to define the entire server application, from a rather low level, as a script.

Such a script can be found on the project's homepage :

This simple web server written in Node responds with "Hello World" for every request.

 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/'); 

To run the server, put the code into a file example.js and execute it with the node program from the command line:

 % node example.js Server running at http://127.0.0.1:1337/ 

Beyond this example, you would want to inspect the req.method and req.url , typically via a router or web framework , to determine how to respond. express or compoundjs would be good options to start with.

You can still use other server applications as an HTTP proxy for Node.js, passing traffic along. But Node.js will still be running separately. If you're using IIS, there's even iisnode which covers much of the setup for this.

Node.js is a server side JavaScript engine which enables you to build web server with JavaScript language. It is similar with PHP on Apache and C# on IIS. One thing different is that Node.js is event based that it does not create many threads as C# IIS does. So, Node.js has the potential of processing high concurrent requests. for Node.js tutorial you may refer to Node.js web site: http://nodejs.org/api/

I then interact with these files via my client-side javascript.

I would disagree on this point with you, because what Node.js do is just create a web server, any interaction between server and client is HTTP based which means Node.js web server responses your requests from client side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM