【问题标题】:How do I always serve the same file with express?如何始终使用 express 提供相同的文件?
【发布时间】:2014-10-12 00:38:42
【问题描述】:

有什么方法可以让我始终提供相同的文件?

因此,如果他们访问 website.com/ajsdflkasjd,它仍然提供与 website.com/asdnw 相同的文件

我想通过 node 使用 express 来做到这一点。

我的文件是静态html文件,不是jade文件。

顺便说一句,如果您想知道,我想要这样做的原因是我有一个 angularjs 应用程序可以为我处理所有路由。所以,我需要做的就是提供一个页面,其余的会由它来处理。

提前致谢!

【问题讨论】:

    标签: node.js angularjs express


    【解决方案1】:

    您可以在角度和节点方面执行此操作。

    在 Node 端你可以这样做:

    res.sendfile('<ur html file path');
    

    在 Angular 中,如果你使用 ui-router,你可以使用

    $urlRouterProvider.otherwise('/otherwise');
    

    另外这个状态也需要定义

    $stateProvider
    .state("otherwise", { url : '/urPage'...})
    

    如果你使用 ngRoute,你可以这样做

     $routeProvider.otherwise({redirectTo: '/urPage'});
    

    更新

    由于您的路由器未配置为显示默认 urPage,因此在服务器中您可以使用以下内容:

    var app = express.createServer();
    app.get('/urPage',function(req,res){
      res.sendfile('<ur html page>');
    });
    

    【讨论】:

    • 嗯,这不是我真正想做的......我的意思是我该怎么做这部分app.get('/[&lt;--this part]',function(req,res){res.sendFile('index.html')})我如何使它成为一个正则表达式或捕捉所有流量的东西并将其重定向到该html页面
    • 你可以使用 app.all 并在里面检查你想要的正则表达式然后发送文件
    【解决方案2】:

    以下是我在项目中使用 express 和 angularjs 的内容。除非浏览器请求包含 extname 的资源文件(图像、css、js 等),否则它将始终发送index.html

    var express = require('express'); var app = express(); 应用程序配置(功能(){ // 网址编码 app.use(express.urlencoded()); // 压缩包 app.use(express.compress()); // 将所有 html 请求重定向到 `index.html` app.use(function (req, res, next) { if (path.extname(req.path).length > 0) { // 正常的静态文件请求 下一个(); } 别的 { // 应该为 angular.js 强制返回 `index.html` req.url = '/index.html'; 下一个(); } }); //静态文件服务 app.use(express.static(__dirname)); });

    【讨论】:

    • Duuuude,非常感谢,太完美了!不幸的是,express 已经弃用了该代码中的一些内容,所以这里是 express 最新版本的更新版本,我只是把它作为一个答案
    【解决方案3】:

    新答案

    const app= require('express')()
         // static file serve
         app.use(express.static(__dirname))
         // not found in static files, so default to index.html
         app.use((req, res) => res.sendFile(`${__dirname}/index.html`))
    app.listen(3000)
    

    旧答案

    var express = require('express');
    var bodyParser = require('body-parser')
    var path = require('path')
    var app = express();
         // url encoding
         app.use(bodyParser.urlencoded({extended:false}));
         // gzip
         // redirect all html requests to `index.html`
         app.use(function (req, res, next) {
             if (path.extname(req.path).length > 0) {
                     // normal static file request
                     next();
                 }
             else {
                     // should force return `index.html` for angular.js
                     req.url = '/index.html';
                     next();
                 }
         });
         // static file serve
         app.use(express.static(__dirname))
    app.listen(3000)
    

    【讨论】:

    • 一年半之后,但这是一个很好的答案。只需通过文件扩展名检查它是否是资产。这so 比我原来的实现简单得多。谢谢赞恩!
    • @Merc 这可能不是我现在会这样做的方式,只是让你知道......我的意思是,它有效,但我可能会先提供静态文件,然后发送索引文件,如果文件不在那里......我会更新我的答案以反映这一点
    【解决方案4】:

    Express 4 的基本配置是:

    var express = require('express');
    
    express()
        .get(/.*/, function(req, res) {
            res.sendFile('index.html', {
                root: __dirname
            });
        })
        .listen(8080);
    

    Working example

    那些带有 GZip、BodyParser 等的 sn-ps 非常酷,但如果你只想测试你的单页应用程序,我认为它过于复杂。当然,您可以在开始需要时添加所有这些“生产资料”。

    阅读更多:

    【讨论】:

      【解决方案5】:

      这里是一个使用 ExpressJs 的简单实现,用于创建虚拟主机并在任何时候返回 index.html

      var express = require('express');
      var app = express();
      var vhost = require('vhost');
      
      // Function to create virtualhost
      function createVhost(domain,rootDirectory){
        var exp = express();
        exp.use(express.static(rootDirectory));
        exp.get(/.*/,function(req,res){
          res.sendFile('index.html',{root:rootDirectory});
      })
      app.use(vhost(domain,exp));
      }
      
      // Virtual Host to create
      createVhost('example.com','/home/[user]/[www]/[example.com]');
      createVhost('othersite.com','/home/[user]/[www]/[othersite.com]');
      
      // Start Server
      app.listen(80,function(){
        console.log('Node server on port 80');
      });
      

      记住:

      在“/etc/host”中添加域(在linux中)

      127.0.0.1  example.com
      127.0.0.1  othersite.com
      

      并在终端中运行“app.js”和“sudo”端口 80

      ~/home/[server]$ sudo node app.js
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 1970-01-01
        • 2021-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多