简体   繁体   English

直接从该node.js应用静态提供的html访问node.js应用功能

[英]Access node.js app functions directly from html served statically from that node.js app

OK so I may be asking too much here and/or showing my naivety, but bear with me. 好,所以我可能在这里要求太多和/或表现出我的天真,但请耐心等待。

At present I have an html (with js) hosted at A, and node.js app hosted at B. The html/js fetches data from the node app via a XMLHttpRequest , and the node app at B dutifully generates the requested data and sends it to A. 目前,我在A托管了一个html(带js),在B托管了node.js应用。html / js通过XMLHttpRequest从节点应用中获取数据,B的节点应用按职责生成了所请求的数据并发送它给A。

I'm trying to reduce the number of http requests generally, and to streamline the performance generally, and wonder whether it's possible to host the html/js via the node app (via express.static() ) at A so that when the html/js requests data from the node server, it's actually requesting data from the same server, and indeed all within the same app (since the node app is generating the data and the node app is also exposing the html/js to a static route). 我正在尝试一般地减少http请求的数量,并总体上简化性能,并且想知道是否有可能通过A节点应用程序(通过express.static() )来托管html / js,以便当html / js向节点服务器请求数据,实际上是从同一服务器,实际上是从同一应用程序中请求数据(因为节点应用程序正在生成数据,并且节点应用程序还将html / js暴露在静态路由中) 。

So is there any way for the js in the html to access the node app functions more directly, ie rather than sending an http request to the same node app, just accesses the data-generating function within the node app directly, or at least without using an http request? 那么html中的js有什么方法可以更直接地访问节点应用程序的功能,即,不是向同一个节点应用程序发送http请求,而是直接访问节点应用程序内的数据生成功能,或者至少没有使用http请求?

I have things set up in my node app so that the html/js can be hosted succesfully via express.static() -- so it's working OK to that extent -- but I just need to know whether it's possible to avoid an http request all the way round a big loop and back to the same node app! 我在节点应用程序中进行了设置,以便可以通过express.static()成功地托管html / js -这样就可以正常工作了-但是我只需要知道是否有可能避免http请求一直绕着一个大循环回到同一个节点应用程序!

The simple answer is, if A and B are far apart, yes, hosting them on the same server will help. 简单的答案是,如果A和B相距遥远,可以,将它们托管在同一台服务器上会有所帮助。

Serving them from the same application won't help as you'll still need to talk via HTTP. 从同一应用程序提供服务无济于事,因为您仍然需要通过HTTP进行交谈。

The question of whether you can remove the HTTP calls from A to B is down to application design. 是否可以从A到B删除HTTP调用的问题取决于应用程序设计。 You have a static web app and an API and you're basically thinking of scraping that and making it one application. 您有一个静态Web应用程序和一个API,并且您基本上是在考虑将其抓取并使其成为一个应用程序。

There are pros and cons to both but I'll be going down the road of personal opinion if I start listing them. 两者都有优点和缺点,但如果我开始列出它们,我会走个人观点的道路。

My vote, don't bother :) 我的投票,不要打扰:)

When you serve html and js files by express.static() they are not running in server, but serve from server to browser. 当您通过express.static()提供html和js文件时,它们不在服务器中运行,而是从服务器提供给浏览器。 And so that js scripts are running from browser. 从而使js脚本从浏览器运行。 Browser scripts to communicate with server, must use http/https requests or sockets. 与服务器通信的浏览器脚本必须使用http / https请求或套接字。 You can communicate browser scripts from server A with server B (but checkout CORS). 您可以将服务器A与服务器B的浏览器脚本进行通信(但结帐CORS)。

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

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