我正在構建一個簡單的REST API(使用PouchDB和Vue.js)。現在,我可以創造projects
有幾個領域:如何將參數傳遞給Express post HTTP方法?
server.js:
var express = require('express')
var PouchDB = require('pouchdb')
var app = express()
var db = new PouchDB('vuedb')
app.post('/projects/new', function(req, res) {
var data = {
'type': 'project',
'title': '',
'content': '',
'createdAt': new Date().toJSON()
}
db.post(data).then(function (result) {
// handle result
})
})
client.js:
// HTML
<input type="text" class="form-control" v-model="title" placeholder="Enter title">
<input type="text" class="form-control" v-model="content" placeholder="Enter content">
<button class="btn btn-default" v-on:click="submit">Submit</button>
// JS
submit() {
this.$http.post('http://localhost:8080/projects/new').then(response => {
// handle response
})
}
如何傳遞參數來設置title
和content
?在REST API中這樣做的傳統方式是什麼?
網址參數:'HTTP://本地主機:8080 /項目/新標題=外國人和內容= scream' ... Express有方法('req.params'我認爲)選擇這些值。 – Andy