简体   繁体   English

在HTML中使用node.js应用

[英]Using a node.js app with HTML

My goal is this: JS but server-side. 我的目标是:JS但在服务器端。 My solution, the obvious, node.js. 我的解决方案是显而易见的node.js。 I've used node.js quiet a bit. 我用了一点安静的node.js。 Mainly for an application, not a web server. 主要用于应用程序,而不是Web服务器。 The only reason I need to do server-side JS is that I need to use a library that connects to the Discord API . 我需要执行服务器端JS的唯一原因是,我需要使用连接到Discord API的库。 So I have a little test .js file with my node.js in it. 所以我有一个带有我的node.js的测试.js文件。 It just prints text if it works. 如果可行,它只会打印文本。 Basic. 基本的。 What I need it to do is whenever someone goes to https://example.com/something , it runs the node.js script and if the script ends up with printing "hello", then https://example.com/something will say "hello". 我需要做的是每当有人访问https://example.com/something时 ,它运行node.js脚本,如果该脚本最终显示“ hello”,则https://example.com/something会说“你好”。

I've done some research on this, I've found ways to deploy a node.js app, which I know how to do. 我对此进行了一些研究,找到了部署node.js应用程序的方法,我知道该怎么做。 I can't really find anything that I'm looking for though. 我找不到真正想要的东西。

You can use express to run a webserver on nodejs 您可以使用express在nodejs上运行Web服务器

  1. Install express by running "npm install express" in your project folder through command prompt 通过命令提示符在项目文件夹中运行“ npm install express”来安装Express
  2. Create a app.js file with the following code 使用以下代码创建一个app.js文件

      var express = require('express'); // load the express library var app = express(); // create an instance of express var child_process = require('child_process'); //load the child_process module app.get("/something", function(req, res) { // Setup a router which listens to the site http://localhost/something child_process.fork("./yourCodeFile.js"); // Launch your code file }); app.listen(80); 
  3. Run node app.js to listen to web connections 运行node app.js来监听Web连接

Then you put your code into the yourCodeFile.js which has to be be in the same folder as the app.js file, even better you could just write all your code in the app.js code as long as you keep it inside the function inside app.get 然后,将代码放入yourCodeFile.js中,该代码必须与app.js文件位于同一文件夹中,甚至更好的是,只要将其保留在函数中,就可以将所有代码写入app.js代码中内部app.get

You should take a look at cloud-based lambda functions and platforms like AWS Lambda , which run a script in response to an HTTP request. 您应该看一下基于云的lambda函数和平台,例如AWS Lambda ,它们会运行脚本来响应HTTP请求。 They are relatively new and the architecture used to support this is being called "serverless", which is a simple term, albeit a bit of a misnomer. 它们相对较新,用于支持此功能的体系结构被称为“无服务器”,这是一个简单术语,尽管有点用词不当。 There are various tools out there to help you build these systems, such as the similarly named Serverless framework, though you can typically still use more traditional server frameworks that you are probably more comfortable with. 有许多工具可以帮助您构建这些系统,例如类似的无服务器框架,尽管您通常仍可以使用更传统的服务器框架,而您可能更喜欢这种框架。 Either way, you are not responsible for managing any server, including starting it or stopping it. 无论哪种方式,您都不负责管理任何服务器,包括启动或停止它。

In terms of constructing a response that you are happy with, you can of course respond with any arbitrary string you want. 就构造您满意的响应而言,您当然可以使用任何所需的字符串进行响应。 See the AWS example of a Node.js handler . 请参阅Node.js处理程序的AWS示例。

exports.myHandler = function(event, context, callback) {
    callback(null, "Hello, world!");
}

Lambda functions can also return binary data and work well with static storage systems like Amazon S3 . Lambda函数还可以返回二进制数据,并且可以与Amazon S3之类的静态存储系统配合使用。 For example, the function can be run in response to the creation of static assets . 例如,该功能可以响应于静态资产创建而运行。

Your code should look like this: 您的代码应如下所示:

const http = require('http');
const url = require('url');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');

      const pathName =url.parse(req.url).pathname;
      if (pathName == '/something') {
          res.end('Hello World\n');
      } else {
          res.end('Please visit /something \n');
      }
});

server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
});

You should run your file with node youfile.js And when you do curl http://127.0.0.1:3000 you will see 您应该使用node youfile.js运行文件。当您curl http://127.0.0.1:3000您将看到

Please visit /something 请访问/某物

But when you do curl http://127.0.0.1:3000/something you will see 但是当您curl http://127.0.0.1:3000/something您会看到

Hello World 你好,世界

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

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