Gulp-Mock-server of the gulp plug-in

Source: Internet
Author: User

This article describes the application of gulp-Mock-server, which is used to virtualize a server and simulate the backend to return JSON data to the front-end. In this way, the frontend and backend can be separated to some extent. After the interface is agreed, both the front and back ends can be developed at the same time to improve efficiency.

Create a task in gulpfile:

//mock servergulp.task(‘mock‘, function() {  gulp.src(‘.‘)    .pipe(g.mockServer({      port: 8090    }));});

Create a data folder in the root directory of the project and put the JSON file in it. For example, we put the test. JSON file in data. You can access http: // localhost: 8090/test in the browser to view the data of test. JSON. In this way, you can also obtain data when Ajax requests the link.

The project directory is as follows:

The following figure shows how to access mock-Server:

The following example shows how to request data through Ajax.

$(function () {    $.get(‘http://localhost:8090/test‘, function (data) {        console.log(data);    });});

So we can see in the console of the browser:

It indicates that we have requested data.

But now I found a problem. The mock address is localhost. How can I access other devices? Therefore, we need to configure the Mosk host and modify the mock task as follows:

//mock servergulp.task(‘mock‘,[‘browser‘], function() {  gulp.src(‘.‘)    .pipe(g.mockServer({      port: 8090,      host: ‘192.168.2.109‘    }));});

So we can see:

Appendix:

I. References

1. Gulp-Mock-server GitHub link https://github.com/sanyueyu/gulp-mock-server

2. Gulp-Mock-server https://www.npmjs.com/package/gulp-mock-server of NPM plug-in

2: Thanks To Cyn, the boss.

Gulp-Mock-server of the gulp plug-in

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.