我正在尝试使用Seneca为笔记创建微服务。我需要通过Postman通过ID获取所有笔记和笔记。到目前为止,由于不需要传递参数,因此我设法获取了所有注释。但是我只能得到一个带有预定义ID的音符。如何在Seneca.js中从Postman接收查询参数?我是塞内卡(Seneca)的新手,所以其他技巧也将不胜感激。提前致谢。 到目前为止,这是我的代码:
const seneca = require('seneca')()
.use('seneca-entity')
const app = require('express')();
const web = require('seneca-web');
const cors = require('cors');
const fs = require('fs');
//get all notes from a json file
const rawnotes= fs.readFileSync('notes.json');
const notes = JSON.parse(rawnotes);
//Store notes in an array
let arr = [];
for (var key in notes.notes) {
if (notes.notes.hasOwnProperty(key)) {
arr.push(notes.notes[key]);
}
}
console.log("ARR[1]:");
console.log(arr[1]);
const Routes = [{
prefix: '/api',
pin: 'role:notes,cmd:*,id:2',
map: {notes: {GET: true},
// http://localhost:8082/api/noteById/id?=2 gets note with id 2, regardless of what id is defined as in Postman
noteById: {GET: true, suffix: '/:id'}}
},{
prefix: '/api',
pin: 'area:categories,action:*',
map: {categories: {GET: true}}
}];
app.use(cors());
var config = {
routes: Routes,
adapter: require('seneca-web-adapter-express'),
context: app,
options: {parseBody: true}
}
seneca.client()
.use(web, config)
.ready(() => {
var server = seneca.export('web/context')()
server.listen('8082', () => {
console.log('server started on: 8082')
})
});
seneca.add({role: 'notes', cmd: 'notes'}, function (args, done) {
try {
done(null, {response: notes})
} catch (err) {
done(err, null)
}
});
seneca.add({role: 'notes', cmd: 'noteById'}, function (msg, reply) {
console.log('MSG.ID IS: ' + msg.id); //2, no matter what is specified in Postman
reply(null, { response: arr[msg.id - 1] })
});
seneca.act({role: 'notes', cmd: 'noteById', id: 2}, function(err, result) {
console.log("RESULT IS: ");
console.log(result);
})
seneca.add({area: 'categories', action: 'categories'}, function (args, done) {
try {
done(null, {response: categories})
} catch (err) {
done(err, null)
}
});
notes.json:
"notes": {
"note1": {
"id": "1",
"name": "a note",
"description": "an example note",
"created date": "20.11.2019",
"modified date": "21.11.2019",
"class": "moderate"
},
"note2": {
"id": "2",
"name": "another note",
"description": "another example note",
"created date": "22.11.2019",
"modified date": "23.11.2019",
"class": "critical"
}
}
}