简体   繁体   English

'node.js':是否需要在服务器上运行?

[英]'node.js': Does it need to be run a server?

I've been following a tutorial http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/ and I would recommend this to anyone that is just getting started with building web applications with node.js. 我一直在遵循http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/的教程,我会向刚开始使用Web构建应用程序的任何人推荐它node.js中 I'm a bit slower though, I guess. 我想我有点慢。 So I wanted to see if the javaScript files created for the application need to be placed on a server. 所以我想看看是否为应用程序创建的javaScript文件需要放置在服务器上。 I do have access to one, and I've searched the web and stackoverflow for this, and what I found was that node.js is a 'javascript runtime';, but I don't completely understand what that means. 我确实可以访问其中一个,并且我已经在网上和stackoverflow上进行了搜索,结果发现node.js是一个“ javascript运行时”;但是我并不完全理解那是什么意思。 I think it means that I don't need to put it on a server, but I just need to some advice from someone with experience. 我认为这意味着我不需要将其放在服务器上,但是我只需要有经验的人的一些建议即可。

Yes there is javascript that is hosted on the server. 是的,服务器上托管着javascript。 This is what is run by the server. 这是服务器运行的内容。 There are also client files that the server code can send to a requester. 服务器代码也可以将客户端文件发送给请求者。 The simple case is when the server and client are the same computer. 最简单的情况是服务器和客户端是同一台计算机。 However in general they are different. 但是总的来说,它们是不同的。

from expressjs . 来自expressjs This is an example of "server code" that is intended to be run on the server. 这是旨在在服务器上运行的“服务器代码”的示例。

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    res.send('Hello World!');
});

var server = app.listen(3000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

if you then point your browser to http://localhost:3000/ you will get back a page that says 如果您随后将浏览器指向http:// localhost:3000 / ,则会返回一个页面,其中显示

Hello World!

The server code is where you would tie into a mongodb server and would package up info to be sent to the client. 服务器代码是您绑定到mongodb服务器并打包信息以发送到客户端的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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