Ratus Rutschi
Ratus Rutschi

Reputation: 27

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 new to JavaScript and don't really understand a lot of it.

This is the HTML code where I want to call the function with the button:

(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.

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

Upvotes: 2

Views: 1423

Answers (1)

In your NodeJS code you have to create a Route that call this function and send to client the response; 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.

Upvotes: 3

Related Questions