Nodejs、koa 路由器、koa 视图(树枝)return "Not found"
Nodejs, koa-router, koa-views (twig) return "Not found"
所以我开始了我的新项目,我的一个朋友建议我放弃 PHP 并尝试使用 Nodejs 和 rethinkDB。
我安装了所有东西,没有路由,它可以工作。但是一旦我添加了路由,我得到了:Not Found
,控制台上没有错误。
包:
"dependencies": {
"keygrip": "^1.0.1",
"koa": "^2.3.0",
"koa-controller": "^1.1.0", //Not used - discontinued ?
"koa-response-time": "^2.0.0",
"koa-router": "^7.2.1",
"koa-views": "^6.0.2",
"rethinkdbdash": "^2.3.29",
"socketio": "^1.0.0",
"twig": "^1.10.5"
}
server.js:
var Koa = require('koa'),
Router = require('koa-router'),
keygrip = require("keygrip"),
r = require('rethinkdbdash')(),
views = require('koa-views'),
Twig = require('twig'),
twig = Twig.twig,
app = new Koa(), // Init Koa
router = new Router(); //Init router
//configure VIEWS
app.use(views(__dirname + '/views/', { extension: 'twig', map: {twig: 'twig' }}))
//Initialize controllers
var ot = require(__dirname+'/controllers/ot.js');
//app.keys = ['im a newer secret', 'i like turtle'];
router
.get('/', async function (ctx, next) {
async (ctx, next) => {
/*ctx.state = {
session: this.session,
title: 'app'
};*/
ctx.render('index', {
message: 'Hello world! <3'
});
};
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
console.log('server listen on http://localhost:3000');
ot.js:
var ot = function() {};
ot.prototype = {
'index' : async function(ctx, next) {
ctx.state = {
session: this.session,
title: 'app'
};
await ctx.render('index', {
message: 'Hello world! <3'
});
console.log(ctx);
}
};
module.exports = new ot();
我的目标是实现如下目标:
router.get('/', master.index);
router.post('/search', master.search);
router.get('/ot', ot.index);
router.get('/ot/:id', ot.getById);
总的 nodejs 菜鸟,非常感谢你的帮助:)
所以这应该有效:对于 index.js 将其更改为:
'use strict';
const Koa = require('koa');
const Router = require('koa-router');
const keygrip = require("keygrip");
// const r = require('rethinkdbdash')(); // not needed in this code at the moment
const views = require('koa-views');
const Twig = require('twig');
const twig = Twig.twig;
const app = new Koa(); // Init Koa
const router = new Router(); //Init router
//configure VIEWS
app.use(views(__dirname + '/views/', { extension: 'twig', map: {twig: 'twig' }}))
//Initialize controllers
var ot = require(__dirname+'/controllers/ot.js');
app.keys = ['im a newer secret', 'i like turtle']; // not needed in this code at the moment
router
.get('/', async function (ctx, next) {
await ctx.render('index', {
message: 'Hello world! <3'
});
});
// routes from ot.js
router.get('/ot', ot.index);
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
console.log('server listen on http://localhost:3000');
然后您的 controllers/ot.js 可能看起来像这样:
'use strict';
exports.index = async function (ctx, next) {
await ctx.render('index', {
message: 'Hello - FROM ot/index'
});
};
exports.someOther = async function (ctx, next) {
// some other function
};
我的样本views/index.twig
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
<h1>My Webpage</h1>
{{ message }}
</body>
</html>
localhost:3000/
和 localhost:3000/ot
两条路线现在应该可以使用了。此处缺少 RethinkDB 部分,因为您没有提供任何示例代码。希望对您有所帮助。
所以我开始了我的新项目,我的一个朋友建议我放弃 PHP 并尝试使用 Nodejs 和 rethinkDB。
我安装了所有东西,没有路由,它可以工作。但是一旦我添加了路由,我得到了:Not Found
,控制台上没有错误。
包:
"dependencies": {
"keygrip": "^1.0.1",
"koa": "^2.3.0",
"koa-controller": "^1.1.0", //Not used - discontinued ?
"koa-response-time": "^2.0.0",
"koa-router": "^7.2.1",
"koa-views": "^6.0.2",
"rethinkdbdash": "^2.3.29",
"socketio": "^1.0.0",
"twig": "^1.10.5"
}
server.js:
var Koa = require('koa'),
Router = require('koa-router'),
keygrip = require("keygrip"),
r = require('rethinkdbdash')(),
views = require('koa-views'),
Twig = require('twig'),
twig = Twig.twig,
app = new Koa(), // Init Koa
router = new Router(); //Init router
//configure VIEWS
app.use(views(__dirname + '/views/', { extension: 'twig', map: {twig: 'twig' }}))
//Initialize controllers
var ot = require(__dirname+'/controllers/ot.js');
//app.keys = ['im a newer secret', 'i like turtle'];
router
.get('/', async function (ctx, next) {
async (ctx, next) => {
/*ctx.state = {
session: this.session,
title: 'app'
};*/
ctx.render('index', {
message: 'Hello world! <3'
});
};
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
console.log('server listen on http://localhost:3000');
ot.js:
var ot = function() {};
ot.prototype = {
'index' : async function(ctx, next) {
ctx.state = {
session: this.session,
title: 'app'
};
await ctx.render('index', {
message: 'Hello world! <3'
});
console.log(ctx);
}
};
module.exports = new ot();
我的目标是实现如下目标:
router.get('/', master.index);
router.post('/search', master.search);
router.get('/ot', ot.index);
router.get('/ot/:id', ot.getById);
总的 nodejs 菜鸟,非常感谢你的帮助:)
所以这应该有效:对于 index.js 将其更改为:
'use strict';
const Koa = require('koa');
const Router = require('koa-router');
const keygrip = require("keygrip");
// const r = require('rethinkdbdash')(); // not needed in this code at the moment
const views = require('koa-views');
const Twig = require('twig');
const twig = Twig.twig;
const app = new Koa(); // Init Koa
const router = new Router(); //Init router
//configure VIEWS
app.use(views(__dirname + '/views/', { extension: 'twig', map: {twig: 'twig' }}))
//Initialize controllers
var ot = require(__dirname+'/controllers/ot.js');
app.keys = ['im a newer secret', 'i like turtle']; // not needed in this code at the moment
router
.get('/', async function (ctx, next) {
await ctx.render('index', {
message: 'Hello world! <3'
});
});
// routes from ot.js
router.get('/ot', ot.index);
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
console.log('server listen on http://localhost:3000');
然后您的 controllers/ot.js 可能看起来像这样:
'use strict';
exports.index = async function (ctx, next) {
await ctx.render('index', {
message: 'Hello - FROM ot/index'
});
};
exports.someOther = async function (ctx, next) {
// some other function
};
我的样本views/index.twig
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
<h1>My Webpage</h1>
{{ message }}
</body>
</html>
localhost:3000/
和 localhost:3000/ot
两条路线现在应该可以使用了。此处缺少 RethinkDB 部分,因为您没有提供任何示例代码。希望对您有所帮助。