我正在尝试将hapi api部署到heroku,并且看起来正确部署,因为它在heroku日志中显示了node bootstrap.js
并且没有失败,但是当我加载应用程序时,什么都没有,只是默认的应用程序登录页面,我如何让heroku显示我的api路由?
package.json
{
"name": "tricity",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node bootstrap.js",
"heroku-postbuild": "node bootstrap.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/runtime-corejs2": "^7.2.0",
"axios": "^0.18.0",
"concurrently": "^4.0.1",
"hapi": "^17.5.4",
"knex": "^0.15.2",
"mysql": "^2.16.0",
"serve": "^10.1.1"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-decorators": "^7.0.0",
"@babel/plugin-proposal-do-expressions": "^7.0.0",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"@babel/plugin-proposal-function-bind": "^7.0.0",
"@babel/plugin-proposal-function-sent": "^7.0.0",
"@babel/plugin-proposal-json-strings": "^7.0.0",
"@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
"@babel/plugin-proposal-numeric-separator": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
"@babel/plugin-proposal-pipeline-operator": "^7.0.0",
"@babel/plugin-proposal-throw-expressions": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-syntax-import-meta": "^7.0.0",
"@babel/plugin-transform-destructuring": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/plugin-transform-spread": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0"
}
}
bootstrap.js
require("@babel/register");
require( './src/server' );
server.js
import Hapi from 'hapi';
import Knex from './knex';
const server = new Hapi.Server({
port: process.env.PORT || 3000,
routes: { cors: true }
})
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return 'Hello, world!';
}
});
server.route({
method: 'GET',
path: '/categories',
handler: function (request, h) {
return Knex( 'categories' ).select( '*' ).then( ( results ) => {
return results
})
}
})
server.route({
method: 'GET',
path: '/sub-categories',
handler: function (request, h) {
return Knex( 'subCategories' ).select( '*' ).then( ( results ) => {
return results
})
}
})
server.route({
method: 'GET',
path: '/products',
handler: function (request, h) {
return Knex( 'products' ).select( '*' ).then( ( results ) => {
return results
})
}
})
// In general, the Knex operation is like Knex('TABLE_NAME').where(...).chainable(...).then(...)
server.start((err) => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});