有什么区别
console.log(process.cwd())
Run Code Online (Sandbox Code Playgroud)
和
console.log(__dirname);
Run Code Online (Sandbox Code Playgroud)
我已经看到两者都在类似的情境中使用过.
我开始尝试使用nodejs/expressjs/coffeescript和jade视图引擎.
我有以下设置,从我可以从周围的示例中看到的看起来非常标准.
app = express.createServer().listen process.env.PORT
app.configure ->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.set 'view options', layout: true
app.use express.bodyParser()
app.use express.static(__dirname + '/public')
app.use app.router
app.get '/ekmHoliCal/index', (req, res) ->
res.render 'index'
Run Code Online (Sandbox Code Playgroud)
我的目录结构如下:
-- ekmHoliCal
-- public
--javascripts
client.js
-- views
index.jade
layout.jade
Run Code Online (Sandbox Code Playgroud)
索引文件只包含行: 这是索引文件
layout.jade文件包含:
!!! 5
html(lang="en")
head
title Users
script(type="text/javascript", src="http://code.jquery.com/jquery-1.6.4.js")
script(type="text/javascript", src="/ekmholical/javascripts/client.js")
body
div!= body
Run Code Online (Sandbox Code Playgroud)
当我导航到localhost/ekmHoliCal/index时,我按预期为索引页面提供服务.但是,如果我查看源代码,我会看到指向jquery的链接:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js">
Run Code Online (Sandbox Code Playgroud)
如果我点击该链接肯定我看到了jquery文件的来源.
索引文件源还包含指向client.js文件的链接,如下所示:
<script type="text/javascript" src="/ekmholical/javascripts/simon.js"></script>
Run Code Online (Sandbox Code Playgroud)
但是当我点击它时,我得到了
无法获得/ekmholical/javascripts/client.js
我已经看到了这个相关的问题(Express-js无法获取我的静态文件,为什么?)并且无法看到我做错了什么
我有一个nodejs服务器运行,客户端封装在客户端文件夹中,以便管理index.html的文件夹结构.然后链接和脚本应该加载没有问题.
client folder
/index.html
/style.css
/script.js
closer folder
/closure
/etc.
app.js
package.json
utility.js
Run Code Online (Sandbox Code Playgroud)
在我的app.js文件中,我有一个正常的
app.get ('/', function (req,res) {
res.render('index.html');
});
Run Code Online (Sandbox Code Playgroud)
当我运行并转到本地主机端口时,文件会加载,但是当我打开chrome控制台时,我看到index.html文件无法加载任何找不到404错误的脚本.我注意到在许多快递应用程序中似乎有一些共同的模式
this app.set('views', __dirname + '/client');
this app.use(express.static(__dirname + "./client"));
this app.use('client', express.directory('client'));
Run Code Online (Sandbox Code Playgroud)
但我没有看到关于app.use和app.set之间差异的解释,也没有一个很好的解释,我能找到的最好的是
app.set('views',__ dirname +'/ views'):使用./views作为客户端模板的默认路径
来自Alex Young的文章,但即使这对我来说有点稀疏和干燥,我希望更深入地解释为什么索引文件可能无法加载与它相同的目录级别的链接.
<link rel="stylesheet" type="text/css" href="style.css" />
Run Code Online (Sandbox Code Playgroud)
我看着这个,我找不到问题.
我目前的目录是
D:\bkp\Programming\TestWorks\nodejs\testApp
Run Code Online (Sandbox Code Playgroud)
但是当我使用__dirname并尝试使用快速服务器显示文件时,它会给我这个错误
Error: ENOENT: no such file or directory, stat 'D:\views\index.html'
Run Code Online (Sandbox Code Playgroud)
我的代码是
res.sendFile(__dirname + 'views/index.html');
Run Code Online (Sandbox Code Playgroud)
当我将它与webpack捆绑在一起并运行捆绑文件时,就会发生这种情况.否则,如果我只是运行正常的app.js文件,它工作正常.帮助将不胜感激.
我正在使用带有节点 js 的 express js 框架,并且在我的 server.js 文件中,我已经使用了
app.use('/api',router);
Run Code Online (Sandbox Code Playgroud)
在我的 ejs 文件中,当我使用脚本标记时
<script src = main.js>
Run Code Online (Sandbox Code Playgroud)
我收到错误消息“无法获取http://localhost:3000/api/main.js ” 如何在 ejs 中包含这些文件
请帮忙!!!
我将服务器的静态index.html与main.css使用此节点服务器:
serve.js:
var express = require('express')
var cors = require('cors')
var app = express()
var path = require('path');
app.use(cors())
app.use(express.static('assets'))
app.get('/', function (req, res, next) {
res.sendFile(path.join(__dirname + '/index.html'));
})
app.listen(3000, function () {
console.log('CORS-enabled web server listening on port 3000')
})
Run Code Online (Sandbox Code Playgroud)
该index.html的:
<!doctype html>
<html class="no-js" lang="">
<head>
<link rel="stylesheet" type="text/css" href="./assets/css/main.css">
</head>
<body>
<p>Hello Html!</p>
<div class="thejson"></div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
main.css
body {
background: #ffdd;
color: #eaeaea;
padding: 10px;
}
Run Code Online (Sandbox Code Playgroud)
结构: …