当我启动具有catbox缓存的hapijs服务器时,会出现以下错误:
找不到模块'/Users/myusername/Documents/Github/cache/index.js'
下面是我正在使用的代码。我如何才能启动该服务器?
此文件的名称为index.js。
我正在使用:
'use strict';
// Load modules
const Boom = require('boom');
const Hapi = require('hapi');
const Joi = require('joi');
内部元素是模块样式指南的一部分
// Declare internals
const internals = {
schema: {
//This sets the limit on the size of the string for the key
key: Joi.string().min(1).max(250),
//this sets the limits for the ttl
ttl: Joi.number().min(100).max(60 * 60 * 1000).default(60 * 1000)
}
};
exports.server = function () {
const server = Hapi.server({
port: 8080,
host: 'localhost'
});
server.app.cache = server.cache({ segment: 'data' });
server.route({ method: 'PUT', path: '/{key}', config: internals.put });
server.route({ method: 'GET', path: '/{key}', config: internals.get });
server.route({ method: 'DELETE', path: '/{key}', config: internals.delete });
return server;
};
server.route({
method: "GET",
path: "/",
handler: (request, h) => {
return "Hello, world!";
}
});
server.route({
method: "GET",
path: "/{name}",
handler: (request, h) => {
return "Hello, " + encodeURIComponent(request.params.name) + "!";
}
});
internals.put = {
validate: {
params: {
key: internals.schema.key
},
query: {
ttl: internals.schema.ttl
}
},
handler: async (request, h) => {
const cache = request.server.app.cache;
await cache.set(request.params.key, request.payload, request.query.ttl);
return h.response().created(`/${request.params.key}`);
}
};
internals.get = {
validate: {
params: {
key: internals.schema.key
}
},
handler: async (request, h) => {
const cache = request.server.app.cache;
const value = await cache.get(request.params.key);
if (value === null) {
throw Boom.notFound();
}
return value;
}
};
internals.delete = {
validate: {
params: {
key: internals.schema.key
}
},
handler: async (request, h) => {
const cache = request.server.app.cache;
await cache.drop(request.params.key);
return null;
}
};
const init = async () => {
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
process.on("unhandledRejection", err => {
console.log(err);
process.exit(1);
});
init();