0
CRUD Logicをルートから取り出して、サービスレイヤーに配置したいと考えています。約束でサービスレイヤーをコーディング
だから、基本的に私はそのようなサービス層を呼び出したい:
function createPost(titel, description, tags, callback) {
const post = {
titel: titel,
description: description,
tags: tags,
createdAt: new Date(),
deleted: false,
}
console.log("Create Post: " + post.titel + " " + post.description + " " + post.tags + " " + post.createdAt + " " + post.deleted)
knex("posts").insert(post, "id").then(id => {
console.log(id)
callback(id[0])
})
}
は現在、私はこの機能を処理するためにcallback
を使用しています:私は、以下の機能を持っている私のpostService.js
ファイルで
const service = require("../service/post")
router.post("/new", (req, res) => {
service.createPost(req.body.titel, req.body.description, req.body.tags, function(id){
console.log("Created post with id: " + id)
res.redirect("index")
})
})
。
idを返すために、より有望なベースのスタイルを使用する方法と、ルータのコードが約束が終わるのを待っていますか?
あなたの返信ありがとうございます!あなたの例では
私は余分なことをしていますが、試しに包み込み以外です。 Node.JSは現在エラーをログに記録しますが、将来的にはアプリケーションを終了することがあります。基本的にnode.jsのすべてのエンドポイントの約束事は本当に扱いが必要です。 – Keith
@Keithエラー処理は簡潔には省略されていますが、はい、私は同意します。サービス層でストレージエラーを完全にカプセル化してください。 – James