不使用 hapi 在 nodejs 中加载 jquery 文件
not loading jquery files in nodejs with hapi
我已经尝试了注释代码中的代码(注释的和未注释的),它显示错误 reply.file is not a function 。在未注释的代码中,它显示错误未知的处理程序文件
我还包括惰性。
server.route({ // Other assets If you have
method: 'GET',
path: '/public/js/{filename}',
handler: {
file: function (request) {
return './public/js/'+request.params.filename;
}
// try{
// console.log(request.params);
// reply.file( './public/js/'+request.params.filename);
// }
// catch(e){
// console.log(e);
// reply.file( './public/js/'+request.params.filename);
// }
}
});
提供静态文件需要 inert 模块
已更新
server.js
'use strict';
const Hapi = require('hapi');
const Inert = require('inert');
const server = new Hapi.Server();
// config
server.connection({
port: 3000
});
// register routes
server.register(Inert, (err) => {
if (err) {
throw err;
}
// default html
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.file('./public/index.html');
}
});
// default /js directory
server.route({
method: 'GET',
path: '/js/{params*}',
handler: {
directory: {
path: './public/js',
listing: false
}
}
});
});
// run
server.start(() => {
console.log('Server running at:', server.info.uri);
});
public/index.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Hapi demo: serving static files</title>
<script type="text/javascript" src="js/foo.js"></script>
</head>
<body>
<h2>see console output.</h2>
</body>
</html>
public/js/foo.js
'use strict';
(function () {
console.log('foo.js has been loaded');
})();
参见:http://hapijs.com/tutorials/serving-files#relative-paths
使用 inert 模块
server.register(Inert, function () {
server.route( {
method: 'GET',
path: '/{param*}',
handler: {
directory: { path: Path.normalize(__dirname + '/') }
}
});
你也可以试试
server.register(Inert, () =>{
server.route( {
method: 'GET',
path: '/public/js/{filename}',
handler: {
file: function (request) {
return __dirname+'/public/js/'+request.params.filename;
}
}
});
});
我已经尝试了注释代码中的代码(注释的和未注释的),它显示错误 reply.file is not a function 。在未注释的代码中,它显示错误未知的处理程序文件 我还包括惰性。
server.route({ // Other assets If you have
method: 'GET',
path: '/public/js/{filename}',
handler: {
file: function (request) {
return './public/js/'+request.params.filename;
}
// try{
// console.log(request.params);
// reply.file( './public/js/'+request.params.filename);
// }
// catch(e){
// console.log(e);
// reply.file( './public/js/'+request.params.filename);
// }
}
});
提供静态文件需要 inert 模块
已更新
server.js
'use strict';
const Hapi = require('hapi');
const Inert = require('inert');
const server = new Hapi.Server();
// config
server.connection({
port: 3000
});
// register routes
server.register(Inert, (err) => {
if (err) {
throw err;
}
// default html
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.file('./public/index.html');
}
});
// default /js directory
server.route({
method: 'GET',
path: '/js/{params*}',
handler: {
directory: {
path: './public/js',
listing: false
}
}
});
});
// run
server.start(() => {
console.log('Server running at:', server.info.uri);
});
public/index.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Hapi demo: serving static files</title>
<script type="text/javascript" src="js/foo.js"></script>
</head>
<body>
<h2>see console output.</h2>
</body>
</html>
public/js/foo.js
'use strict';
(function () {
console.log('foo.js has been loaded');
})();
参见:http://hapijs.com/tutorials/serving-files#relative-paths 使用 inert 模块
server.register(Inert, function () {
server.route( {
method: 'GET',
path: '/{param*}',
handler: {
directory: { path: Path.normalize(__dirname + '/') }
}
});
你也可以试试
server.register(Inert, () =>{
server.route( {
method: 'GET',
path: '/public/js/{filename}',
handler: {
file: function (request) {
return __dirname+'/public/js/'+request.params.filename;
}
}
});
});