0

If I were to serve the localhost root file a HTML file like so:

app.get('/', (req, res) => res.sendfile('index.html'))

Can I add Javascript files to the HTML document that can't be viewed or touched by the browser? And if so, is it possible that it can also have access to node api's?

Im new to Express so I have no clue how it works.

undefinedChar
  • 537
  • 1
  • 5
  • 18
  • 3
    No, that is not possible. JavaScript runs either in the browser (which means, the browser needs to access it) or on the server. – str Jun 18 '19 at 20:14
  • 2
    No, this is not possible. – APAD1 Jun 18 '19 at 20:14
  • 3
    Based on a little interpretation of what you said - no and no. But I suspect you might not have meant what you said. Are you really asking about serving the content from the server or executing it on the server? – symcbean Jun 18 '19 at 20:15
  • Mostly related: https://stackoverflow.com/questions/6869312/how-do-i-hide-javascript-code-in-a-webpage – VLAZ Jun 18 '19 at 20:16
  • 2
    If the browser can't view it, how does it render it? And what is your objective? Are you wanting to prevent users from being able to intercept the script after it loads? If so, why? And if you are new to express, should worrying about hypothetical user behavior that you probably can't actually control be your highest priority? – Anthony Jun 18 '19 at 20:17
  • you can use an obfuscator like this one: https://www.obfuscator.io/ – ControlAltDel Jun 18 '19 at 20:26
  • I was more speaking of having code that can send normal DOM methods to the HTML page, but the browser just has no way of seeing it... It's mostly to be able to use node api's and send the result to the browser... – undefinedChar Jun 18 '19 at 20:32
  • It's simply not possible. The browser executes DOM methods, it can't execute them without seeing them. – Barmar Jun 18 '19 at 20:37
  • 1
    It's like telling someone to call you, but not giving them your phone number. – Barmar Jun 18 '19 at 20:38
  • Okay, let me get this straight there is no way for the main node file to communicate with any DOM/HTML Javascript or however you wanna say it...? – undefinedChar Jun 18 '19 at 20:39

1 Answers1

1

You can have the server do some work after receiving parameters from the frontend. The javascript loaded in the DOM will send a request to the server, the server will do some work, unknown to the frontend JS, then return the result.

On Server:

app.post('/path', (req, res) => {
    const json = req.body;
    //do work
    const resp = {some: 'data'};
    res.json(resp);
}

On Frontend

fetch('/path', {
  method: 'post',
  body: JSON.stringify(data),
  headers: { 'Content-type': 'application/json' }
})
.then(res => res.json()) // get json data out of response object
.then(json = > {
    // do something with response json
}

You'll want to do some reading on Express and body parsing, as well as using parameters in GET requests as opposed to body in POST requests, also other types of requests.

Ace
  • 1,028
  • 2
  • 10
  • 22
  • Sounds exactly like what I wanted, if you know Electron my thought was there must be something similar to ipc, and I guess there is... I’ll take a look it later today! One other thing is it possible for the DOM to ask the main to do something? So basically the reverse of the above. – undefinedChar Jun 19 '19 at 05:14
  • What I wrote above has the frontend/client (DOM) requesting information from the backend/server (main). If you want the server to send data to the client without being prompted for information (and vice-versa), then you can look into using socket.io. This package allows you to send data from server/client using `socket.emit('event_name', optional_json)` And listen for data using `socket.on('event_name', data => doWork(data))` – Ace Jun 19 '19 at 11:42
  • Then I believe you've found what you're looking for. Remember to mark my response if it answered your question. – Ace Jun 19 '19 at 12:09