【问题标题】:Node JS Configuration节点 JS 配置
【发布时间】:2015-05-16 20:49:06
【问题描述】:

我正在尝试配置我的节点 js 以运行我的 html 代码。我也在使用引导程序和 Express Js。当我运行节点 js 时,它没有加载 css。谁能帮助我可能是什么问题。这是节点js代码sn-p。

var express = require("express");
var app     = express();
var path    = require("path");
app.get('/',function(req,res) {
    res.sendFile(__dirname + '/home.html')
})
app.use(express.static(__dirname + '/public'))
app.listen(3000);

console.log("Running at Port 3000");

当我直接加载 HTML 文件时,它会正确加载 CSS,但是当我使用 node js 加载它时,它会失败。问题的原因可能是什么?

【问题讨论】:

  • 您不应该在 app.get() 处理程序中调用 app.use()。放在外面。
  • 您好,谢谢您的回复。我尝试了您的建议,但我仍然面临同样的问题。您能告诉我可能是什么问题吗?
  • edit您的问题反映您的尝试。
  • 我已经编辑了问题以反映我尝试过的内容你能告诉我可能是什么问题吗?
  • 当您尝试直接在浏览器中加载 css 文件时会发生什么?

标签: html node.js twitter-bootstrap express


【解决方案1】:

检查您的目录结构是否正确,并且您已授予 Node.js 进入目录和读取文件的正确权限。

如果你的目录结构是这样的:

/public
    /stylesheets
        home.css
home.html
server.js

您的 server.js 代码如下所示:

var express = require("express");
var app     = express();
var path    = require("path");
app.get('/',function(req,res) {
    res.sendFile(__dirname + '/home.html');
})
app.use(express.static(__dirname + '/public'));
app.listen('3000', function() { 
    console.log("Listening on port 3000"); 
});

当你运行这个时:

node ./server.js

并在浏览器中访问此 URL:

http://localhost:3000/stylesheets/home.css

您将返回 home.css 文件。

【讨论】:

  • app.listen('3000',function(){ console.log("listening at 3000"); });
【解决方案2】:

在 express js 项目中,您可以根据需要放置静态文件。

app.use('/static', express.static('public'))
Now, you can load the files that are in the public directory from the /static path prefix.

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html

https://expressjs.com/en/starter/static-files.html 检查此链接以了解如何使用 express js 连接静态文件

【讨论】:

    【解决方案3】:

    在express js项目中配置数据库

    /config
      /database.js
    /server.js
    /.env
    
    const http = require('http');
    const app = require('express')();
    require('./config/database.js');
    const bodyParser = require('body-parser');
    const server = http.createServer(app);
    
    server.listen(process.env.ServerPort, '0.0.0.0', () => {
      logger.info(`Express server listening on port ${process.env.ServerPort}`);
    });
    

    当你运行这个时:

    node server.js
    

    database.js 文件

    const My = require('jm-ez-mysql');
    
    // Init DB Connection
    const connection = My.init({
      host: process.env.DBHOST,
      user: process.env.DBUSER,
      password: process.env.DBPASSWORD,
      database: process.env.DATABASE,
      dateStrings: true,
      charset: 'utf8mb4',
      timezone: 'utc',
      multipleStatements: true,
      connectTimeout: 100 * 60 * 1000,
      acquireTimeout: 100 * 60 * 1000,
      timeout: 100 * 60 * 1000,
    });
    
    module.exports = {
      connection,
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 2015-02-04
      • 1970-01-01
      相关资源
      最近更新 更多