我不明白为什么,即使我已经更新了所有内容,我也有解决方案。使用任何解决方案,我都有相同的错误。 我只是尝试在其他文档中传送,我已经尝试了不使用Joi.object 我的服务代码位于文档index.js中,而我的验证正试图在route.js中进行 我也在尝试用typesafe-joi来做,但是没用
Argument of type '{ [n: number]: { method: string; path: string; handler: (req: any, h: any) => any; } | { path: string; method: string; options: { validate: { payload: ObjectSchema<any>; }; }; handler: (req: any, h: any) => Promise<...>; } | { ...; } | { ...; }; ... 31 more ...; includes
Promise<...>; } | { ...; } | { ...; }; ... 31 more ...; includes(searchElement: { ...; } | ... 2 more ... | { ...' is not assignable to type 'ServerRoute[]'.
The types of 'pop().options' are incompatible between these types.
Type '{ validate: { payload: Joi.ObjectSchema<any>; }; }' is not assignable to type 'RouteOptions | ((server: Server) => RouteOptions)'.
The types of 'validate.payload' are incompatible between these types.
Type 'ObjectSchema<any>' is not assignable to type 'RouteOptionsResponseSchema'.
Type 'ObjectSchema<any>' is not assignable to type 'SchemaMap'.
Index signature is missing in type 'ObjectSchema<any>'.ts(2345)
routes.js
'use strict'
const Joi = require('@hapi/joi')
const site = require('./controllers/site')
const user = require('./controllers/user')
module.exports = [
{
method: 'GET',
path: '/',
handler: site.home
},
{
method: 'GET',
path: '/register',
handler: site.register
},
{
path: '/create-user',
method: 'POST',
options: {
validate: {
payload: Joi.object({
name: Joi.string().required().min(3),
email: Joi.string().email().required(),
password: Joi.string().required().min(6)})
}
},
handler: user.createUser
},
{
method: 'GET',
path: '/login',
handler: site.login
},
{
path: '/validate-user',
method: 'POST',
options: {
validate: {
payload: Joi.object({
email: Joi.string().email().required(),
password: Joi.string().required().min(6)
})
}
},
handler: user.validateUser
},
{
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
index: ['index.html']
}
}
}
]
index.js
'use strict'
const Hapi = require('@hapi/hapi')
const handlerbars = require('handlebars')
const inert = require('@hapi/inert')
const path = require('path')
const routes = require('./routes')
const vision = require('@hapi/vision')
const { runInThisContext } = require('vm')
const server = Hapi.server({
port: process.env.PORT || 3000,
host: 'localhost',
routes: {
files: {
relativeTo: path.join(__dirname, 'public')
}
}
})
async function init () {
try {
await server.register(inert)
await server.register(vision)
server.views({
engines: {
hbs: handlerbars
},
relativeTo: __dirname,
path: 'views',
layout: true,
layoutPath: 'views'
})
server.route(routes);
await server.start()
} catch (error) {
console.error(error)
process.exit(1)
}
console.log(`Servidor lanzado en: ${server.info.uri}`)
}
init()