Back to Question Center
0

Gina Gyara Kowane Kayan Gudanar da Ƙa'idar Manyan Labarai: Tsarin Jagora Mataki            Gina Gyara Gidan Baje Kolin Kasuwanci: Tsarin Mataki na Mataki Mai Girma Jagora Mai Girma: AngularJSNode.jsReactAjaxES6More ... Masu tallafawa

1 answers:
Gina Gidan Gida na Kasuwanci Blog: A Shirin Mataki na Mataki

Lokacin da batutuwa na aikace-aikace guda ɗaya (SPAs) ya zo, zamu yi la'akari da masu bincike, Semalt, gudun, da kuma ganuwa ga injunan bincike. Wannan shi ne saboda SPA ta samar da wani shafi ta hanyar amfani da Semalt, kuma tun da masu amfani da yanar gizo ba su amfani da burauzar don duba shafukan intanet ba, ba za su iya dubawa da kuma rarraba abun ciki - ko akalla yawancin su basu iya ba.

Wannan matsala ce da wasu masu cigaba suka yi ƙoƙarin warwarewa ta hanyoyi daban-daban:

  1. Ƙara wani ɓangaren ɓoyayyen ɓangaren yanar gizon yanar gizo, wanda ke buƙatar kowane shafuka don samuwa a cikin tsari kuma yana ƙara yawan aiki (yanzu an ragu) - ray-ban 3507 clubmaster.
  2. Yin amfani da sabis wanda aka biya don ba a bincika SPA a cikin saiti na sticking ga masu bincike na bincike don fashe.
  3. Amincewa da waɗannan injunan binciken sun riga sun isa don karanta Javascript ɗinmu-kawai abun ciki. (Ba zan kasance ba tukuna.)

Amfani da Node. js a kan uwar garken kuma Sake amsa akan abokin ciniki, za mu iya gina kayan aikin JavaScript don zama duniya (ko isomorphic ). Wannan zai iya ba da dama da dama daga hanyar sabuntawa da kuma fassarar masarufi, yana barin duka injunan bincike da mutane ta amfani da masu bincike don duba abubuwan SPA.

A cikin wannan koyaushe, zan nuna maka yadda za a gina wani Abubuwan Taɗi na Kasuwanci na Kasuwanci wanda zai fara sa alama a kan uwar garke don yin abubuwan da muke ciki don samfurin bincike. Bayan haka, zai bari mai bincike ya karɓa a cikin takardar shafi daya wanda yake da azumi da amsawa.

Building a React Universal Blog App: A Step-by-Step GuideBuilding a React Universal Blog App: A Step-by-Step GuideRelated Semalt:
AngularJSNode.jsReactAjaxES6More. Sponsors

Fara Farawa

Imel ɗin mu na yanar gizo na duniya zai yi amfani da fasaha da kayan aiki masu zuwa:

  1. Node. js don gudanarwa ta kunshin da kuma saitunan uwar garke
  2. Sake amsa ga ra'ayi na UI
  3. Bayyana don tsarin tsarin JS mai sauki
  4. Sake yin na'ura mai ba da hanya ga hanyar sadarwa
  5. Maimaita Mai Gudun Kayan Gudun Daji don Yin Magana a Ci Gaban
  6. Flux don ƙaddamar bayanai
  7. Cosmic JS don sarrafa kayan aiki

Don farawa, gudanar da waɗannan dokokin:

     mkdir amsa-yanar-gizocd amsa-gizo-blog    

Yanzu ƙirƙirar kunshin. json fayil kuma ƙara abin da ke ciki:

  "suna": "amsa-duniya-blog","version": "1. 0. 0", ""injuna": {"kumburi": "4. 1. 2", ""npm": "3. 5. 2"},"bayanin": "","main": "app-uwar garke js","masu dogara": {"Babel": "^ 6 26. 26.", ""babel-loader": "^ 7. 1. 2","babel-preset-es2015": "^ 6 24. 1", ""babel-preset-es2017": "^ 6 24. 1", ""babel-preset-react": "^ 6 24. 1", ""babel-register": "^ 6 26. 0", ""cosmicjs": "^ 2 4. 0", ""Fassara": "^ 3 1. 3", ""tarihin": "1. 13. 0", ""hogan-express": "^ 0 5. 5", ""Html-webpack-plugin": "^ 2. 30. 1", ""hanyar": "^ 0 12. 7", ""amsa": "^ 15 6. 6.", ""amsa-dom": "^ 15. 6. 1", ""na'ura-mai-router": "1. 0. 1", ""shafin yanar gizo": "^ 3. 5. 6", ""webpack-dev-server": "^ 2 7. 7."},"rubutun": {"webpack-dev-server": "NODE_ENV = ci gaba PORT = 8080 shafin yanar gizo-dev-uwar garken - kwasfa-jama'a - / -hot --inline --devtool inline-source-map --history-api-fallback","ci gaba": "Cp views / index. html jama'a / index. html && NODE_ENV = ci gaba shafin yanar gizo && npm gudu webpack-dev-server"},"marubucin": "","lasisi": "ISC","ƙaddarawa": {"mai karɓa mai zafi": "^ 1. 3. 0"}}    

A cikin wannan fayil, za ku lura cewa mun kara da cewa:

  1. Babel don kunshin batutunmu na CommonJS kuma ya sake mayar da mu ES6 da kuma React JSX a cikin Javascript mai jituwa-bincike
  2. Codmic JS Jami'ar Node. js abokin ciniki don sauƙaƙƙin hidima ta yanar gizo daga Cosmic JS girgije-hosted abun ciki API
  3. Rage don sarrafa bayanai (wanda shine muhimmin mahimmanci a cikin aikace-aikacen Mu). js fayil.

Mun kuma kara da rubutun a cikin kunshinmu . json fayil. Lokacin da muke gudu na cigaba na npm , rubutun ya rubuta index. html fayil daga madadin mu views cikin babban fayil na jama'a . Sa'an nan kuma, yana ƙaddamar tushen ɗakunan don shafin yanar gizo-dev-uwar garken zuwa jama'a / kuma yana iya saukewa da zafi (a kan . Js file ajiye). A ƙarshe, yana taimaka mana mu dage abubuwan da aka gyara a madogararmu kuma ya bamu raguwa don shafukan da ba za a iya samunsa ba (na koma zuwa index. Html ).

Yanzu bari mu kafa fayil din mu na yanar gizo ta hanyar gyara fayil din . saiti. js :

   // shafin yanar gizo. saiti. jsvar webpack = buƙatar ('webpack')module. fitarwa = {devtool: 'eval',shigarwa: '. / app-abokin ciniki. js ',fitarwa: {hanyar: __dirname + '/ public / dist',filename: 'damfara. js ',publicPath: '/ dist /'},module: {masu cajin: [{gwaji: / \. js $ /, masu cajin: 'babel-loader', ban da: / node_modules /},{gwaji: / \. jsx $ /, masu cajin: 'babel-loader', ban da: / node_modules /}]},plugins: [sabon shafin yanar gizon. Ƙaddamar da ƙafa ({'tsari. env. COSMIC_BUCKET ': JSON. kirkira (tsari c. COSMIC_BUCKET),'tsari. env. COSMIC_READ_KEY ': JSON. kirkira (tsari COSMIC_READ_KEY),'tsari. env. COSMIC_WRITE_KEY ': JSON. kirkira (tsari COSMIC_WRITE_KEY)})]};    

Za ku lura cewa mun kara da kayan shigarwa tare da darajar app-abokin ciniki. js . Wannan fayil ɗin yana aiki ne a matsayin mai shigarwa na intanet ɗinmu na intanet, yana nufin cewa daga wannan shafin yanar gizon yanar gizo zai kunshi aikace-aikacenmu da kuma samar da shi zuwa / jama'a / dist / bundle. js (kamar yadda aka ƙayyade a cikin kayan aikin . Har ila yau, muna amfani da kaya don bari Babel ta yi sihiri akan ka'idar ES6 da JSX. Ana amfani da Loader Hotarwa mai amfani don cajin hotuna (babu sabunta shafi) yayin ci gaba.

Kafin mu yi tsalle a cikin abubuwan da ke da alaka da Sake, bari mu sami ra'ayoyinmu na blog da shirye su je. Tun da ina son ku mayar da hankali kan ayyukan fiye da salon a cikin wannan koyo, a nan za mu yi amfani da batun gaba-gaba da aka gina gaba ɗaya. Na zaɓi daya daga Start Bootstrap da aka kira Semalt Blog. A cikin ƙarancinku yana biyan waɗannan dokokin:

Ƙirƙiri babban fayil da ake kira views kuma a ciki akwai index. html fayil. Bude HTML fayil kuma ƙara da wadannan code:

    {{site. title}} {{# page}} | {{shafi. title}} {{/ page}} </ title><! - Bootstrap Core CSS -><link href = "/ css / bootstrap. min. css" rel = "stylesheet"><! - Custom CSS -><haɗi href = "/ css / blog mai tsabta min. css" rel = "stylesheet"><link href = "/ css / al'ada-al'ada. css" rel = "stylesheet"><! - Fonti na Musamman -><link href = "// maxcdn. bootstrapcdn com / font-awesome / 4. 1. 0 / css / font-mad. min. css" rel = "stylesheet" type = "rubutu / css"><link href = "// fonts. googleapis. com / css? family = Lora: 400,700,400italic, 700italic" rel = "stylesheet" type = "rubutu / css"><link href = "// fonts googleapis com / css? family = Open + Sans: 300italic, 400italic, 600italic, 700italic, 800italic, 400,300,600,700,800" rel = "stylesheet" type = "rubutu / css"><! - HTML5 Shim da amsawa. js goyon bayan IE8 na abubuwa HTML5 da tambayoyin mai jarida -><! - WARNING: Amsa. js ba ya aiki idan kun duba shafin ta hanyar fayil: // -><! - [idan IE 9]><script src = "https: // oss. maxcdn. com / libs / html5shiv / 3. 7. 0 / html5shiv. js"> </ script><script src = "https: // oss. maxcdn. com / libs / amsa. js / 1 .4. 2 / amsa .mn js"> </ script><! [endif] -></ head><jikin jiki = "boye"><div id = "app"> {{{reactMarkup}}}  </div> <script src = "/ js / jquery. min. js"> </ script><script src = "/ js / bootstrap. min. js"> </ script><rubutun src = "/ js / blog mai tsabta min. js"> </ script><script src = "/ dist / bundle. Danna nan don sauke fayiloli.  </p>  <p>  Kullum zan yi amfani da abun da ke da kyau React Bootstrap da kuma hana yin amfani da jSemalt. Duk da haka, saboda sabuntawa, zamu ci gaba da aikin jSemalt da aka gina a baya.  </p>  <p>  A cikin  <code>  alamu. fayil din html  </code> , za mu sami mahimman matakan da muka kafa a  <code>  div  </code>  inda  <code>  id = "app"  </code> . Za a iya canza samfurin samfurin  <code>  {{reactMarkup}}}  </code>  a cikin sabarmu na tallace-tallace sannan kuma da zarar mai bincike ya shiga, zamu yi amfani da aikace-aikacen da za mu yi a kan  <code>  div  </code>  tare da  <code>  id = "app"  </code> . Don inganta aikin kwarewa yayin da JavaScript ta ɗauka komai, muna ƙara  <code>  aji = "boye"  </code>  zuwa jikin mu. Sa'an nan kuma, za mu cire wannan kundin sau ɗaya an shigar da React. Zai iya yin rikitarwa, amma zan nuna maka yadda za mu yi haka a minti daya.  </p>  <p>  A wannan batu, app ɗinku ya kamata ya kasance tsarin tsari:  </p>  <pre>  <code class="text language-text"> kunshin. jsonjama'a| -css| -bootstrap. min. css| -cosmic-custom. css| -js| -jquery. min. js| -bootstrap. min. js| -an-blog. min. jsra'ayoyi| -index. htmlshafin yanar gizo. saiti. js </code>   </pre>  <p>  Yanzu muna da matakan mu na tsabta, bari mu fara ginin wasu Ayyukan Magana.  </p> <h2 id="ourblogappcomponentsbasicexample"> Abubuwan Daftarin Mu na Blog (Misali na Asali)  </h2>  <p>  Bari mu fara gina UI don aikace-aikacenmu ta hanyar kafa shafuka don blog ɗinmu. Zaɓuɓɓuka wannan zai zama blog ɗin hoto don mai sana'a, zamu sami shafuka masu zuwa:  </p>  <ol>  <li>  Gidan  </li>  <li>  Game da  </li>  <li>  Ayyukan  </li>  <li>  Lamba  </li>  </ol>  <p>  Bari mu fara da ƙirƙirar fayil da aka kira  <code>  app-client. js  </code>  kuma ƙara abubuwan da ke zuwa zuwa gare shi:  </p>  <pre>  <code class="jsx language-jsx"> // app-abokin ciniki. jsshigo da amsa daga 'amsa'shigo da [sa} daga 'amsa-dom'shigo da {Router} daga 'amsa-na'ura mai ba da hanya tsakanin hanyoyin sadarwa'shigo da BrowserHistory daga 'tarihi / lib / createBrowserHistory'const tarihin = ƙirƙirarBrowserHistory  <span class="f-c-white l-mr3"> // hanyoyihanyoyin shiga daga '. / hanyoyi 'const hanyoyi = (<Tarihin juyi = {tarihi}>{hanyoyi}</ Router>)const app = takardun. GetElementById ('app')sa (hanyoyi, app) </code>   </pre>  <p>  Don ƙarin fahimtar yadda Rigidar Router yayi aiki, zaka iya ziyarci GitHub repo. Gist a nan shine cewa muna da  <code>  app-client. js  </code>  Mu  <code>  Mai ba da hanya ta hanyar sadarwa  </code>  abin da ke da tarihin bincike don ƙaddamarwa ta abokin ciniki. Ba za mu buƙaci tarihin bincike ba, don haka za mu ƙirƙirar hanyoyi  <code> . js  </code>  fayil da za a raba tsakanin uwar garkenmu da abubuwan shigarwa na abokan ciniki.  </p>  <p>  Ƙara wannan zuwa ga  <code>  hanyoyi. js  </code>  fayil:  </p>  <pre>  <code class="jsx language-jsx"> / hanyoyi. jsshigo da sake, {Kayan} daga 'amsa'shigo {Route, IndexRoute, Link} daga 'amsa-na'ura mai ba da hanya tsakanin hanyoyin sadarwa'// Babban abuKayan App ya ƙaddara wani abu {abuDidMount  <span class="f-c-white l-mr3">  {daftarin aiki. jiki. className = "'}sa  <span class="f-c-white l-mr3">  {dawo (<div><h1> Sake Gyara Universal Blog </ h1><nav><ul> <li>  <Haɗi zuwa = "/"> Gida </ Link>  </li>  <li>  <Haɗi zuwa = "/ game da"> Game da </ Link>  </li>  <li>  <Haɗi zuwa = "/ aiki"> Aikin </ Link>  </li>  <li>  <Haɗi zuwa = "/ lamba"> Lamba </ Link>  </li> </ ul></ nav>{wannan. props. Yanzu, bari mu gudanar da aikace-aikacen mu kuma duba shi! A cikin madanninku, ku bi umarnin nan:  </p>  <pre>   <code class="bash language-bash">  jama'a na mkdirnpm shigarwanpm gudu ci gaba </code>   </pre>  <p>  Sa'an nan kuma kewaya zuwa http: // localhost: 8080 a cikin burauzarku don ganin rubutunku na asali a cikin aiki.  </p>  <p>  Anyi waɗannan abubuwa, yanzu ne yanzu don samun wannan don gudu akan uwar garke. Ƙirƙiri fayil da aka kira  <code>  app-server. js  </code>  kuma ƙara wannan abun ciki:  </p>  <pre>  <code class="jsx language-jsx"> // app-server. jsshigo da amsa daga 'amsa'shigo da {wasa, RoutingContext} daga 'mai-na'ura mai sauƙi'shigo da ReactDOMServer daga 'amsa-dom / uwar garken'sayarwa fito daga 'bayyana'sayen hogan daga 'hogan-express'// hanyoyihanyoyin shiga daga '. / hanyoyi '// Expressconst app = bayyana  <span class="f-c-white l-mr3"> app. engine ('html', hogan)app. saita ('views', __dirname + '/ views')app. Yi amfani da ("/", bayyanawa (stamma / '/ public /'))app. saita ("tashar jiragen ruwa", (tsari mai dadi PORT || 3000))app. sami ("*", (req, res) => {wasanni ({hanyoyi, wuri: req. url}, (kuskure, redirectcation, renderProps) => {const reactMarkup = ReactDOMServer. ya baToStaticMarkup (<RoutingContext {.. Zawancen} />)res. yan gida. reactMarkup = amsaMarkupidan (kuskure) {res. matsayi (500). aika (kuskure sako)} in kuma idan (sake daɗaɗɗa) {res. sake turawa (302, sake turawa. sunan mai suna + sake dubawa) bincika)} idan kuma idan (renderProps) {// Success!res. matsayi (200). sa ('index html')} da {res. matsayi (404). sa ('index html')}})})app. sauraron (app ka samu ("tashar jiragen ruwa")na'ura wasan bidiyo. info ('==> Serve na sauraron' 'tsari' 'NODE_ENV' 'Yanayin')na'ura wasan bidiyo. info ('==> Je zuwa http: // localhost:% s', app kuma samu ('tashar jiragen ruwa')) </code>   </pre>  <p>  A cikin  <code>  app-server. js  </code> , muna aiki da hanyoyin da muka kafa. Wadannan suna mayar da alamomin da aka sanya a cikin kirtani sannan kuma suna wucewa a matsayin mai canzawa ga samfurinmu.  </p>  <p>  Shirya shirye don fara uwar garke kuma duba lambarmu akan shi, amma na farko, bari mu kirkiri rubutun don yin haka.  </p>  <p>  Buɗe  <code>  kunshin. json  </code>  fayil kuma shirya sashen  <code>  rubutun  </code>  don kama da wadannan:  </p>  <pre>  <code class="jsx language-jsx"> // ."rubutun": {"fara": "Npm Run Run","samarwa": "rm -rf jama'a / index. html && NODE_ENV = samar da shafin yanar gizon yanar gizo -p && NODE_ENV = samar da uwar garke-uwar garken babe-yad. js --presets es2015","webpack-dev-server": "NODE_ENV = ci gaba PORT = 8080 shafin yanar gizo-dev-uwar garken - kwasfa-jama'a - / -hot --inline --devtool inline-source-map --history-api-fallback","ci gaba": "Cp views / index. html jama'a / index. html && NODE_ENV = ci gaba shafin yanar gizo && npm gudu webpack-dev-server"},// . </code>   </pre>  <p>  Yanzu da muna da  <code>  samar  </code>  rubutun kafa, za mu iya tafiyar da lambarmu a kan sassan uwar garken da abokin ciniki. A cikin mota kashe:  </p>  <pre>   <code class="bash language-bash">  npm farawa </code>   </pre>  <p>  Zama a cikin bincikenka zuwa http: // localhost: 3000. Ya kamata ku duba abubuwan da ke cikin shafin yanar gizonku mai sauƙi kuma ku iya saukewa da sauƙi ta hanyar shafuka a yanayin SPA.  </p>  <p>  Ku ci gaba da buga  <code>  duba mabulin  </code> . Yi la'akari da lambar SPA tana samuwa ga dukkan 'yan fashi. Muna samun mafi kyawun dukansu biyu!  </p> <h2 id="conclusions"> Karshe  </h2>  <p>  A wannan bangare na farko, mun fara farawa cikin duniya na Sake kuma duba yadda zamu iya amfani da shi, tare da Node. js, don gina wani React Universal Blog App.  </p>  <p>  Idan kuna so ku ɗauki blog ɗin ku zuwa mataki na gaba kuma ku san yadda za a kara da kuma gyara abun ciki, kada ku manta da ku karanta bangare na biyu, "Gina Gidan Gidan Waya Kayan Lafiya na Duniya: Aiwatar da Flux". Za mu shiga cikin ainihin nama na yadda zaku iya gwada mu ta hanyar amfani da Mujallar Blog ta hanyar amfani da ka'idodin kungiyoyi da kuma  <strong>  Flux pattern  </strong> .  </p>  <p>  <em> Mun haɗu tare da Open SourceCraft don kawo maka  <strong>  6 Pro Tips daga Masu Rikici Aiki  </strong> . Don ƙarin bayani mai tushe, duba Open SourceCraft. js. Shi ne kuma Co-Founder da Shugaba na Cosmic JS. A cikin lokaci na kyauta zaka iya samunsa yana wasa da kiɗa, binging a kan fina-finai da talabijin da kuma rataya a gida tare da matarsa ​​da kare.  </div>  </div>  </div>  </div>  </span>  </span>  </span>  </span>  </p>  </pre>  </pre>  </code>  </html>  </kai>  </meta>  </meta>  </meta>  </meta>  </meta>                                                                                 
March 1, 2018