无论我在 Express 中如何设置 res.format(),数据总是根据 fetch() 的解析方法来解析。为什么?
No matter how I set the res.format() in Express, the data is always parsed depending on the parse method of fetch(). Why?
我不明白 res.format()
是如何工作的。我阅读了所有文档,以及关于 Whosebug 的几个答案,但它们实际上并没有解决让我感到困惑的部分。
app.get('/', (req, res) => {
res.format({
'text/html' : function() {
res.send({name: 'romeo'})
}
})
})
在上面的例子中,我将格式设置为'text/html'
,但我实际上发送了一个JS object。
所以,现在,在客户端:
fetch(req)
.then(res=>res.json())
.then(content=> {
//output.innerHTML = JSON.stringify(content, '\n', 2);
console.log(content)
output.innerHTML = content;
})
.catch(err=>console.eror);
如果我使用 json()
,无论我发送什么,也无论我将 res.format()
设置成什么,数据都会被解析为 JS object。为什么?这是我不明白的。
即使我把格式设置成'application/json'
,我也只能发送一个纯文本,然后如果我再用json()
,它又会return一个JSobject.
反之亦然,如果我发送 JS object,但使用 text()
,它将被解析为文本。
那么,格式到底有什么作用?
是的,我读到它应该检查HTTP请求header中的Accept
,然后根据Accept
值调用相应的处理程序,但事实并非如此告诉我任何事。我不是设置请求header,不管我怎么设置format()
,数据的实际格式总是由我选择的解析方式决定的,对JS使用json()
object,或 text()
用于文本或 html。我看不出设置 format()
.
的目的
res.format(object)
用于使客户端可以指定他们想要返回的内容类型。客户根据他们的请求使用 Accepts
header 指定此信息。
要了解有关 res.format(object)
的更多信息,请访问 https://expressjs.com/en/5x/api.html#res.format
要阅读有关 Accepts
header 的更多信息,请访问 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
下面我创建了一个简单的例子来说明它的样子
服务器代码
const express = require("express");
const PORT = process.env.PORT || 5000;
const app = express();
app.get('/', function (req, res) {
res.format({
'text/plain': function () {
res.send('text')
},
'text/html': function () {
res.send('<p>html</p>')
},
'application/json': function () {
res.send({ message: 'json' })
},
})
})
app.listen(PORT, () => {
console.log("Listening on port: " + PORT);
});
客户代码
const fetch = require("node-fetch");
fetch('http://localhost:5000/', {
headers: {
'Accept': 'text/html'
}
})
.then(res => res.text())
.then(res => {
console.log(res) // <p>html</p>
});
fetch('http://localhost:5000/', {
headers: {
'Accept': 'text/plain'
}
})
.then(res => res.text())
.then(res => {
console.log(res) // text
});
fetch('http://localhost:5000/', {
headers: {
'Accept': 'application/json'
}
})
.then(res => res.json())
.then(res => {
console.log(res) // { message: 'json' }
});
我不明白 res.format()
是如何工作的。我阅读了所有文档,以及关于 Whosebug 的几个答案,但它们实际上并没有解决让我感到困惑的部分。
app.get('/', (req, res) => {
res.format({
'text/html' : function() {
res.send({name: 'romeo'})
}
})
})
在上面的例子中,我将格式设置为'text/html'
,但我实际上发送了一个JS object。
所以,现在,在客户端:
fetch(req)
.then(res=>res.json())
.then(content=> {
//output.innerHTML = JSON.stringify(content, '\n', 2);
console.log(content)
output.innerHTML = content;
})
.catch(err=>console.eror);
如果我使用 json()
,无论我发送什么,也无论我将 res.format()
设置成什么,数据都会被解析为 JS object。为什么?这是我不明白的。
即使我把格式设置成'application/json'
,我也只能发送一个纯文本,然后如果我再用json()
,它又会return一个JSobject.
反之亦然,如果我发送 JS object,但使用 text()
,它将被解析为文本。
那么,格式到底有什么作用?
是的,我读到它应该检查HTTP请求header中的Accept
,然后根据Accept
值调用相应的处理程序,但事实并非如此告诉我任何事。我不是设置请求header,不管我怎么设置format()
,数据的实际格式总是由我选择的解析方式决定的,对JS使用json()
object,或 text()
用于文本或 html。我看不出设置 format()
.
res.format(object)
用于使客户端可以指定他们想要返回的内容类型。客户根据他们的请求使用 Accepts
header 指定此信息。
要了解有关 res.format(object)
的更多信息,请访问 https://expressjs.com/en/5x/api.html#res.format
要阅读有关 Accepts
header 的更多信息,请访问 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
下面我创建了一个简单的例子来说明它的样子
服务器代码
const express = require("express");
const PORT = process.env.PORT || 5000;
const app = express();
app.get('/', function (req, res) {
res.format({
'text/plain': function () {
res.send('text')
},
'text/html': function () {
res.send('<p>html</p>')
},
'application/json': function () {
res.send({ message: 'json' })
},
})
})
app.listen(PORT, () => {
console.log("Listening on port: " + PORT);
});
客户代码
const fetch = require("node-fetch");
fetch('http://localhost:5000/', {
headers: {
'Accept': 'text/html'
}
})
.then(res => res.text())
.then(res => {
console.log(res) // <p>html</p>
});
fetch('http://localhost:5000/', {
headers: {
'Accept': 'text/plain'
}
})
.then(res => res.text())
.then(res => {
console.log(res) // text
});
fetch('http://localhost:5000/', {
headers: {
'Accept': 'application/json'
}
})
.then(res => res.json())
.then(res => {
console.log(res) // { message: 'json' }
});