表达式不自动提供索引.html静态文件



这是我的目录结构:

build/  (contains gulp tasks for generating dist from src)
dist/ (this is the static app I'm serving)
  (all other assets, css, images, etc)
  index-5c1755df65.html
src/  (source files, can't be served through express)
express/ (express app + API)
  app.js

这是我的表达app.js

//Enable Helmet security fixes - dnsPrefetchControl, clickjacking prevention, poweredBy hide, HSTS, ieNoOpen, MIME type sniffing and XSS prevention
app.use(helmet());
//Parse the body of the request as a JSON object, part of the middleware stack (https://www.npmjs.com/package/body-parser#bodyparserjsonoptions)
app.use(bodyParser.json());
//Serve static Angular JS assets from distribution, part of the middleware stack, but only through HTTPS
//--------The following line used to work!---------
app.use('/', ensureSecure, express.static('dist');
//Import routes (Removed getToken router. BH Token is processed through a helper)
app.use('/api', [router_invokeBhApi]);
//404 Redirect on unhandled url
app.use('*', function (req, res, next) {
    res.status(404).sendFile(__dirname + '/views/404.html');
});
//Setup port for access
app.listen(process.env.PORT || 3000, function () {
    console.log(`The server is running on port ${process.env.PORT || 3000}!`);
});

我曾经在上面的代码中使用该行为整个dist(这是一个角度1.4.3应用程序)提供服务,它曾经提供索引文件,即使它的名称中有一个随机密码。

经过一些更新后,它就停止工作了。现在我必须这样做,使用一个名为 serve-static from npm 的外部库:

app.use('/', ensureSecure, serveStatic('dist', {'index': ['index-5c1755df65.html', 'index.html']}));

不知道如何解决这个问题,或者是什么导致了这种突然的变化。是浏览器吗?还是棱角分明?(根据我的最新更改从1.4.3升级到1.5.8

编辑:我的html文件

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <!--REMOVED ALL THE META TAGS FOR STACK OF PURPOSES-->
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title>IMG Career Portal</title>
        <!-- default font -->
        <!--<link href='//fonts.googleapis.com/css?family=Roboto:400,700,500' rel='stylesheet' type='text/css'>-->
        <link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
        <!-- Font Awesome -->
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="styles/vendor-f398fee98b.css">
        <link rel="stylesheet" href="styles/app-292a67e7f9.css">
    </head>
    <body>
        <!-- Main Application Tag (Angular2 Ready :)) -->
        <main></main>

        <script src="scripts/vendor-ec12e9202e.js"></script>
        <script src="scripts/app-8e0fb1a5c9.js"></script>
    </body>
</html>

我运行gulp express来清理、构建和运行应用程序:

gulp.task('express', ['clean','build'], function(){
  nodemon({
      script: 'express/app.js'
  });
});

这是因为app.js需要在head标签内工作。

如果需要索引.html请使用重点 API 初始化文件:

app.get('/', function(req, res){
   res.render("../yourpaste/index"); //the new index.ejs file
        });

在应用中添加.js

module.exports = app;

尝试将你的应用.js放在头标签内。

<!doctype html>
<html lang="en">
    <head>
        <script src="scripts/app-8e0fb1a5c9.js"></script>  //your app.js
        <meta charset="utf-8">
        <meta name="description" content="Official career portal of the Ian Martin Group, an Engineering & IT recruiting B Corporation.">
        <meta property="og:title" content="Ian Martin Group: Careers">
        <meta property="og:description" content="Find the next great step in your Engineering or IT career here.">
        <meta property="og:image" content="https://media.glassdoor.com/o/de/b1/5f/51/the-ian-martin-group-all-company-retreat-2015.jpg">
        <meta property="og:url" content="https://careers.ianmartin.com">
        <meta name="twitter:title" content="Ian Martin Group: Careers">
        <meta name="twitter:description" content="Find the next great step in your Engineering or IT career here.">
        <meta name="twitter:image" content="https://media.glassdoor.com/o/de/b1/5f/51/the-ian-martin-group-all-company-retreat-2015.jpg">
        <meta name="twitter:card" content="summary_large_image">
        <meta property="og:site_name" content="IMG Careers">
        <meta name="twitter:image:alt" content="Great company culture.">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title>IMG Career Portal</title>
        <!-- default font -->
        <!--<link href='//fonts.googleapis.com/css?family=Roboto:400,700,500' rel='stylesheet' type='text/css'>-->
        <link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
        <!-- Font Awesome -->
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="styles/vendor-f398fee98b.css">
        <link rel="stylesheet" href="styles/app-292a67e7f9.css">
    </head>
    <body>
        <!-- Main Application Tag (Angular2 Ready :)) -->
        <main></main>

        <script src="scripts/vendor-ec12e9202e.js"></script>

    </body>
</html>

相关内容

最新更新