如何在以下代码中添加Get或POST请求。我的代码运行良好。但是,如何添加GET,POST,PUT或DELETE之类的请求
var expect = require('chai').expect;
var request = require('request');
describe('Status and content', function() {
describe ('Main page', function() {
it('status', function(done){
request('http://localhost:3000/', function(error, response, body) {
expect(response.statusCode).to.equal(200);
done();
});
});
it('content', function(done) {
request('http://localhost:3000/' , function(error, response, body) {
//expect(body).to.equal('Hello World');
done();
});
});
});
describe ('About page', function() {
it('status', function(done){
request('http://localhost:3000/', function(error, response, body) {
expect(response.statusCode).to.equal(200);
done();
});
});
});
});
此外,在运行此代码之前,我如何安装和运行JSON服务器,以便api在本地主机上就绪。因此,我不必手动进行操作。 https://medium.com/codingthesmartway-com-blog/create-a-rest-api-with-json-server-36da8680136d?fbclid=IwAR2mEtB6-BKAsSgUto3aOTjx8WmAbsfKB6RkSvHeZbI4Jt0fiqMwbV_QvGw
答案 0 :(得分:0)
您可以通过这种方式使用请求模块。
let options = {};
options.method = 'GET';
options.uri = 'http://localhost:3000/';
request(options, function(err, res, body){
//....
});
也用于“ POST”,
let options = {};
options.method = 'POST';
options.uri = 'http://localhost:3000/';
options.body = {payload};
options.headers = {}; //headers if any
request(options, function(err, res, body) {
//.....
});