繁体   English   中英

如何在以下代码中添加Get或POST请求

[英]How to add Get or POST request in the following code

如何在以下代码中添加 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-BKAsSgUto3aOTjx8WmAbsfKB6RkSvHeZbI4Jt0fiqMwGwv_

您可以通过这种方式使用请求模块。

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) {
//.....
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM