私はウェブ開発に新しいので、どんな助力もありがとうございます。斧を使ってエクスプレスサーバーに文字列を送る方法
プレーンテキストをサーバーに送信してMathJaxを適用する必要があります。第
サーバ側sendForTeX (text) {
console.log('Sending ' + text + ' to be converted to LaTeX...')
axios.post('/tolatex', text)
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
}
、I、使用、body-parser
app.use(bodyParser.urlencoded({ extended: false }))
そして、私がテキストを植字するミドルウェアを定義します。
function toLatex (req, res, next) {
console.log(req.body)
MathJax.typeset({
math: req.body,
format: 'TeX',
svg: true,
mml:false,
}, function (data) {
if (!data.errors) {
req.LaTeX = data
return next()
} else {
console.log('BRRRRUUU')
}
})
}
最後に、私は新しいデータが
app.post('/tolatex', toLatex, function (req, res) {
res.send(req.LaTeX)
})
いくつかの文献を読んだ、私の理解は、私はクライアントから送信するテキストはreq.body
になるということです戻って送信することでPOSTリクエストを処理しますが、 console.log(req.body)
の出力は、text: ''
の形式のオブジェクトです。text
は、クライアントから送信されたテキストです。その後、ノードはすぐにメッセージでクラッシュします。TypeError: Cannot convert object to primitive value
私はここでいくつかの指針をいただきありがとうございます。
UPDATE:一部突っつい周り
app.use(bodyParser.urlencoded({ extended: false }))
が
var jsonParser = bodyParser.json()
app.use(jsonParser)
に置き換える必要があるとプレーンテキストがオブジェクトe.g. {plaintext: text}
に包まれるべきであることを信じるように私をリード。これはうまくいくようです。私は単純な文字列をPOSTできないと結論づけるのは正しいでしょうか?
'req.body'には何が含まれているのかを表示できますか? –
私はこの 'math:req.body、'は 'math:req.body.text'であるべきだと思います –
@SagarJajoriya yup、ありがとう! –