Giter Site home page Giter Site logo

typescript-isomorphic-request-in-node-and-web-demo's Introduction

TypeScript Isomorphic Request in Node and Web Demo

如何定义一个isomorphic的request库,可以在node与web环境同时使用。

关键点:

  1. 使用declare module声明了一个用于占位的module: #request,并定义了相关的接口
  2. 使用request库(node)与jquery(web)分别实现该#requset接口
  3. 使用两个webpack的配置,分别定义alias,将#request替换为该环境下真实的实现

Node

npm install
npm run demo-node

它可以正常打包、运行、并打印出得到的结果。

Web

npm run demo-web

它打包完成后,将在浏览器中打开生成的dist/index.html文件,从浏览器的console中可以看到如下错误:

Access to XMLHttpRequest at 'https://postman-echo.com/get?hello=request' from origin 'null' 
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present 
on the requested resource.

说明代码已经正常运行向目标网站发出请求,只是因为浏览器的安全策略被限制。

typescript-isomorphic-request-in-node-and-web-demo's People

Contributors

freewind avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.