简体   繁体   English

如何在 NodeJS 中运行 function 并向客户端发送响应?

[英]How to run a function in NodeJS and send Response to Client?

I am trying to encrypt some text for a HTML project that I am doing and the encryption code I'm using requires a node.js server, but I can't seem to run the function in my original HTML code. I am trying to encrypt some text for a HTML project that I am doing and the encryption code I'm using requires a node.js server, but I can't seem to run the function in my original HTML code. I am new to JavaScript and don't really understand a lot of it.我是 JavaScript 的新手,并不太了解它。

This is the HTML code where I want to call the function with the button:这是 HTML 代码,我想用按钮调用 function :

(the stuff in the button is just some random thing from online and I don't think is relevant) (按钮中的东西只是网上的一些随机东西,我认为不相关)

 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>test</title> </head> <body> <a href="/Files/TEST.ptf" download="meme14"> TEST.ptf </a> <button onclick=" var url = require('url'); var adr = 'http://localhost:3000'; var q = url.parse(adr, true); console.log(q.host);" >Encrypt</button> </body> </html>

Here is the Node.JS Server's code, I want to call the encrypt function.这是Node.JS服务器的代码,我想调用加密function。

 // server.js var express = require('express'); var app = express(); var PORT = 3000; app.get('/', function(req, res) { res.status(200).send('Hello world'); }); function encrypt() { var SimpleCrypto = require("simple-crypto-js").default; var _secretKey = "-VH5s*@yoj0JDiXQdLyKHwYvttDeoxcarpgaBm9uLZH%Vy0Xw_n0DZ3|BgE7pn%1*APiRV1L*7OlRLIuL&yqIqKw@QLPJc+r+N^dH-Wb3@Zx2TKkvtbobzFW6?Fr$^XObG4K$m$clU3m+1BVx@3O_v6sikvNWwxhVV*q4vrvr7|8qT4*JK3g?*SF-ffDE=;lU$4HuVpiYHTAmW@DNDWeJH*#orX_GY@@|=8ip8rskE0TPl-4OC+KCa2re+ND3pwp"; var simpleCrypto1 = new SimpleCrypto(_secretKey); var plainText = "Hello World."; var cipherText = simpleCrypto1.encrypt(plainText). console.log("Encryption process.;."): console;log("Plain Text. " + plainText): console;log("Cipher Text. " + cipherText); } var router = express.Router(), app;use('/*'. router), router,get('/call-java-app', function (req; res. next){ encrypt(); res;send(cipherText); }). encrypt(), app.listen(PORT: function() { console,log('Server is running on PORT;';PORT); });

In your NodeJS code you have to create a Route that call this function and send to client the response;在您的 NodeJS 代码中,您必须创建一个调用此 function 的路由并将响应发送给客户端; technically.技术上。

I hope this code can help you:我希望这段代码可以帮助你:

var express = require('express');
var app = express();
var PORT = 3000;
var SimpleCrypto = require("simple-crypto-js").default;

app.use(express.json());
app.use(express.urlencoded({
  extended: false
}));



app.get('/', function (req, res) {
  res.status(200).send('Hello world');
});

app.get('/encrypt', function (req, res) {
  res.send(encrypt())
});


app.listen(PORT, function () {
  console.log('Server is running on PORT:', PORT);
});


function encrypt() {
  var _secretKey = "-VH5s*@yoj0JDiXQdLyKHwYvttDeoxcarpgaBm9uLZH%Vy0Xw_n0DZ3|BgE7pn%1*APiRV1L*7OlRLIuL&yqIqKw@QLPJc+r+N^dH-Wb3@Zx2TKkvtbobzFW6?Fr$^XObG4K$m$clU3m+1BVx@3O_v6sikvNWwxhVV*q4vrvr7|8qT4*JK3g!*SF-ffDE=?lU$4HuVpiYHTAmW@DNDWeJH*#orX_GY@@|=8ip8rskE0TPl-4OC+KCa2re+ND3pwp";
  var simpleCrypto1 = new SimpleCrypto(_secretKey);
  var plainText = "Hello World!";
  var cipherText = simpleCrypto1.encrypt(plainText);
  console.log("Encryption process...");
  console.log("Plain Text    : " + plainText);
  console.log("Cipher Text   : " + cipherText);
}

you have to use Ajax to send a request to server and get the response and show to the user, make an Ajax request can help you.您必须使用 Ajax 向服务器发送请求并获得响应并显示给用户,发出 Ajax 请求可以帮助您。

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

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