我正在使用Linux Mint OS。我正在react.js中学习 public class BasicHttpServerExample2 {
public static void main(String[] args) throws IOException {
int port = 88;
InetSocketAddress socket = new InetSocketAddress("192.168.0.37", port);
HttpServer server = HttpServer.create();
server.bind(socket, port);
System.out.println("server started at " + port);
HttpContext rootContext = server.createContext("/");
HttpContext context = server.createContext("/print");
rootContext.setHandler(BasicHttpServerExample2::RootHandler);
context.setHandler(BasicHttpServerExample2::handleRequest);
server.start();
}
public static void RootHandler(HttpExchange he) throws IOException {
String response = "<h1>Server start success if you see this message</h1>" + "<h1>Port: " + "88" + "</h1>";
he.sendResponseHeaders(200, response.length());
OutputStream os = he.getResponseBody();
os.write(response.getBytes());
os.close();
}
private static void handleRequest(HttpExchange exchange) throws IOException {
/** handler resoponse **/
URI requestURI = exchange.getRequestURI();
printRequestInfo(exchange);
/** handler resoponse **/
String response = "Data Received";
exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
/** handler resoponse **/
exchange.sendResponseHeaders(200, response.getBytes().length);
/** handler resoponse **/
OutputStream os = exchange.getResponseBody();
/** handler resoponse **/
os.write(response.getBytes());
/** handler resoponse **/
os.close();
}
private static void printRequestInfo(HttpExchange exchange) {
/** Console ***/
System.out.println("-- headers --");
Headers requestHeaders = exchange.getRequestHeaders();
requestHeaders.entrySet().forEach(System.out::println);
/** Console ***/
System.out.println("-- principle --");
HttpPrincipal principal = exchange.getPrincipal();
System.out.println(principal);
/** Console ***/
System.out.println("-- HTTP method --");
String requestMethod = exchange.getRequestMethod();
System.out.println(requestMethod);
/** Console ***/
System.out.println("-- query --");
URI requestURI = exchange.getRequestURI();
String query = requestURI.getQuery();
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(query + " received at " + formatter.format(date));
}
}
。我使用Testing
创建了一个应用。我使用create react app
安装了jest-cli。我的文件夹结构如下
我的测试如下所示
App.test.js
sudo npm i -g jest-cli
我的package.json如下所示
package.json
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});
describe('Addition', () => {
it('knows that 2 and 2 make 4', () => {
expect(2 + 2).toBe(4);
});
});
我遇到错误
我读了this SO post。按照该帖子的说明,我按照以下步骤操作
但是我仍然遇到错误。
答案 0 :(得分:1)
您不需要安装jest-cli
,这可能是导致错误的原因。 create react app
已经开玩笑了,请尝试从全局依赖项中删除jest-cli
并运行npm test
。
答案 1 :(得分:1)
删除所有的node_modules(删除node modules文件夹)。然后使用myhost/second
或yarn
安装它们,使用npm install
或npm test
命令运行测试。