所以我开始了我的新项目,我的一个朋友召集我抛弃PHP并尝试使用rethinkDB尝试Nodejs。
我安装了所有东西,没有路线,它的工作原理。但是一旦我添加路由,我得到: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);
Total nodejs noob,我真的很感谢你的帮助:)。
答案 0 :(得分:0)
所以这应该有效:对于 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部分,因为您没有提供任何示例代码。希望有所帮助。