[英]Serving static JavaScript file in Hapi.js not working
I have created a static route like this: 我创建了这样的静态路由:
"use strict";
var StaticPlugin = {
register: function (server, options, next) {
server.route([
{
method: 'GET',
path: '/public/{path*}',
config: {
auth: false,
description: 'Static assets',
handler: {
directory: {
path: '../public',
index: false,
listing: false
}
}
}
}
]);
return next();
}
};
StaticPlugin.register.attributes = {
name: 'StaticPlugin',
version: '1.0.0'
};
module.exports = StaticPlugin;
I register the plugin, but when I attempt to try to load a static JS file (going to be used to serve static js libraries), I get a 404 error. 我注册了插件,但是当我尝试加载静态JS文件(将用于提供静态js库)时,出现404错误。
The url I am trying to hit is http://localhost:3000/public/javascripts/mylib.js
我想打的网址是
http://localhost:3000/public/javascripts/mylib.js
In your configuration the path is relative to the directory where the process is running. 在您的配置中,路径是相对于进程正在运行的目录的。 If this plugin is in a subdirectory and you start the server in the main directory, then the path will be incorrect.
如果此插件位于子目录中,而您在主目录中启动服务器,则路径将不正确。 Either use a path that is relative to the project root, or use
Path.join()
in conjunction with __dirname
, eg: 请使用相对于项目根目录的路径,或者将
Path.join()
与__dirname
结合使用,例如:
var Path = require('path');
Path.join(__dirname, '../public')
您必须确保使用插件“ Inert”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.