我在 React 中有一个客户端应用程序,在 Node 中有一个服务器(使用 Express)。在服务器端,我有一个如下所示的端点(不是真正的端点,只是我在做什么的一个想法):
function endpoint(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain',
'Transfer-Encoding': 'chunked'
});
for(x < 1000){
res.write(some_string + '\n');
wait(a_couple_of_seconds); // just to make process slower for testing purposes
}
res.end();
}
Run Code Online (Sandbox Code Playgroud)
这是完美的,我的意思是,当我调用这个端点时,我收到了包含所有 1.000 行的整个流。
问题是我无法通过块(对于每个“写入”或一堆“写入”)来获取这些数据,以便在我收到它们后立即在前端显示......(想想一个表格一旦我从端点调用中获得它们就会显示行)
在前端,我使用 Axios 通过以下代码调用 API:
async function getDataFromStream(_data): Promise<any> {
const { data, headers } = await Axios({
url: `http://the.api.url/endpoint`,
method: 'GET',
responseType: 'stream',
timeout: 0,
});
// this next line doesn't work. it says that 'on' is not a function …Run Code Online (Sandbox Code Playgroud) 我需要创建许多类的新实例.我正在使用反射但是当我创建c.newInstance()时,没有'empty parameter constructor'的类会抛出java.lang.InstantiationException.
现在,我该怎么做才能创建每个类的实例?
我知道我可以使用c.getConstructor(Class).newinstance(params)创建没有'空参数构造函数'的类的实例,但我不知道每个类的参数.
还有一件事,所有这些类都扩展自另一个名为ParentClass的类,因此我可以使用的一种解决方法是在ParentClass中包含一些强制子类实现"空参数构造函数"的代码,但不知道如何做这个.
提前致谢 !