【发布时间】:2017-01-27 05:05:34
【问题描述】:
我正在使用 node.js 在本地运行(要启动我正在使用的服务器:终端中的“node index.js”),当我尝试链接我的 array.js 文件时,我不断收到此错误。
GET http://localhost:3000/array.js 404 (Not Found)
如果重要的话,我的文件路径就是这样......
/Campus/views(Index.pug file)
/Campus(Index.js file)
/Campus/public/js(array.js file)
我的 HTML(pug 语法)标题:
doctype html
head
script(src='array.js', type='text/javascript')
link(rel='stylesheet', type='text/css', href='http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css')
link(rel='stylesheet', type='text/css', href='http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css')
我觉得我确实在这里正确地引用了我的 .js 文件,但这是我第一次使用 node.js,也许有一些我正在寻找的东西。
我的 Index.js 代码:
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 3000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', 'views');
app.set('view engine', 'pug');
app.get('/', function(request, response) {
response.render('index', {
title: 'Homepage'
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
【问题讨论】:
-
你可能“感觉”你做到了,但你没有。
script(src='js/array.js', type='text/javascript') -
嘿,就是这样!非常感谢!
-
仅供参考,静态文件来自公用文件夹,因此请使用
public/之后的路径。 -
哦,好吧。会做
标签: javascript html node.js pug