简体   繁体   English

节点表示未获取bootstrap.js文件

[英]node express not getting bootstrap.js file

I have a directory structure: 我有一个目录结构:

projectName
    | -.sencha/
    | - app/
        | - view
        | - controller
        | - store
    | - saas
    | - server/
        | -server.js
    | - index.html
    | - bootstrap.js
    | - app.js

I would like to start my app and serve index.html with node. So in server/server.js I have:

app.get('/', function(req, res) {
  res.sendFile('index.html', { root: path.join(__dirname, '../') });
})

And this is working fine but after this my localhost throwing an error bootstrap.js is not found.

index.html index.html

    <!DOCTYPE HTML>
    <html manifest="">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta charset="UTF-8">

        <title>serverApp</title>

        <!-- The line below must be kept intact for Sencha Cmd to build your application -->
        <script id="microloader" type="text/javascript" src="bootstrap.js"></script>

    </head>
    <body></body>
    </html>

So can you tell me how to solve this? 那你能告诉我如何解决吗? or else Can you provide me a good link of example which is developed in extjs,express,nodejs,mongodb. 否则,您能给我提供extjs,express,nodejs,mongodb中开发的示例的良好链接吗?

You should rewrite your file structure a bit to serve static files: 您应该稍微重写文件结构以提供静态文件:

Add this line to your server.js to serve static files from your public folder: 将此行添加到server.js以从public文件夹中提供静态文件:

app.use('/', express.static('public'));

And modify your file structure to this: 并将您的文件结构修改为:

projectName
    | -.sencha/
    | - app/
        | - view
        | - controller
        | - store
    | - saas
    | - server/
        | -server.js
    | - public/
        | - index.html
        | - bootstrap.js
        | - app.js

more info: http://expressjs.com/en/starter/static-files.html 更多信息: http : //expressjs.com/en/starter/static-files.html

I 100% agree with @Adam on this one! 我对此100%同意@Adam! Additionally, you should resolve your public folders to absolute paths, its not a must but guidelines so as we don't get punches ... ;) 另外,您应该将公用文件夹解析为绝对路径,这不是必须的,而是准则,以免造成麻烦...;)

You should restructure your app as @Adam suggested, plus 您应该按照@Adam的建议重组应用,再加上

Do the following, it will resolve your issue with good guidelines. 请执行以下操作,它将通过良好的指导原则解决您的问题。

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var app = express();


 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());



//This will enable you to access it form '/'
app.use('/', express.static(__dirname + '/public', {index: "index.html"}));

// Rest of the stuff

Then if you will visit your URL that you set and port, you'll be able to access. 然后,如果您访问设置和移植的URL,则可以访问。

Using express.static is recommended way of serving static content. 建议使用express.static来提供静态内容。

Hope it helps! 希望能帮助到你!

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

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