简体   繁体   English

如何在expressJS服务器中从客户端提供index.html静态文件?

[英]How to serve index.html static file from client-side within expressJS server?

Front-end: 前端:

I have a dist/index.html file generated by Webpack to serve the user. 我有一个由Webpack生成的dist/index.html文件来为用户服务。

Back-end nodeJs server side: 后端nodeJs服务器端:

I also have a nodeJs server which want to serve static files from server include index.html from server. 我也有一个nodeJs服务器,它想从服务器提供静态文件,包括服务器提供的index.html。

app.use(express.static('server-side-public-folder'))
router.get('/*', (req, res) => {
    res.render('index.html', {secret: secret1});
});

Question: 题:

Is there any way to serve index.html from client-side instead of server-side like this? 有没有办法像这样从客户端而非服务器端提供index.html

app.use(express.static('http://remote.com'))
router.get('/*', (req, res) => {
    res.render('http://remote.com/index.html', {secret: secret1});
});

thanks! 谢谢!

Probably static sites hosting is what you need. 您可能需要静态站点托管。 Look for these: 寻找这些:

1) Netlify (my favorite) 1) Netlify (我的最爱)

2) Zeit 2) Zeit

3) Firebase Cloud 3)Firebase云

If I understand your question, you want to "Host" a website? 如果我理解您的问题,您想“托管”一个网站吗?

Typically with hosting you would register your DNS info: "remote.com", with some DNS provider like GoDaddy. 通常,通过托管,您可以向一些DNS提供商(例如GoDaddy)注册DNS信息:“ remote.com”。 From there you could point that at your home network/server via your Fios/Comcast allocated IP. 从那里,您可以通过Fios / Comcast分配的IP将其指向家庭网络/服务器。 Be aware that if you are not 'business class' the IP address tends to change every so often. 请注意,如果您不是“企业级”,则IP地址会经常更改。

If you don't want to deal with all that, check out the ones listed above or Heroku (which is free). 如果您不想处理所有这些,请查看上面列出的内容或Heroku(免费)。

I think you are confused 我觉得你很困惑

  1. if you already have index.html in your dist folder you can use express.static('dist') which would automatically render index.html file. 如果dist文件夹中已经有index.html,则可以使用express.static('dist'),它会自动呈现index.html文件。

  2. if you want to get index.html from thirdparty that you don't have direct access you have to get it by using 'http' module or other npm libraries. 如果要从没有直接访问权的第三方获取index.html,则必须使用“ http”模块或其他npm库来获取它。 (make sure you have their permission) (确保获得他们的许可)

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

相关问题 如何配置expressjs以在生产模式下提供index.html? - How to configue expressjs to serve index.html in production mode? 从Promise对象(服务器端)获取价值到客户端index.html - Get value from promise object (serverside) into client-side index.html 将文件从客户端移动到服务器 - move a file from the client-side to the server 如何在express.js中提供从服务器引用js文件并在客户端运行它的html? - How to serve html in express.js that references js file from server and run it on client side? Index.html 而不是服务器端的客户端内容 - Index.html instead of client stuff on server side 由于路径相对性问题,无法从快递服务器提供angular 2 index.html文件 - Cannot serve angular 2 index.html file from express server due to path relativity issue 如何使用没有客户端javascript的JSON数据呈现静态HTML? - How to render static HTML with JSON data with no client-side javascript? 如何设置简单静态服务器,其中路径“ /”指向目录中的index.html? - How to setup simple static server where path '/' points to index.html within dirrectory? 如何从客户端 Javascript 文件中访问 Express app.locals 变量? - How can I access an Express app.locals variable from within a client-side Javascript file? ExpressJS 重定向索引.html - ExpressJS redirect index.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM