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
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:
Ƙ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).
Yin amfani da sabis wanda aka biya don ba a bincika SPA a cikin saiti na sticking ga masu bincike na bincike don fashe.
Amincewa da waɗannan injunan binciken sun riga sun isa don karanta Javascript ɗinmu-kawai abun ciki - ecig battery charger 4. (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.
Imel ɗin mu na yanar gizo na duniya zai yi amfani da fasaha da kayan aiki masu zuwa:
Node. js don gudanarwa ta kunshin da kuma saitunan uwar garke
Sake amsa ga ra'ayi na UI
Bayyana don tsarin tsarin JS mai sauki
Sake yin na'ura mai ba da hanya ga hanyar sadarwa
Maimaita Mai Gudun Kayan Gudun Daji don Yin Magana a Ci Gaban
Flux don ƙaddamar bayanai
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:
Babel don kunshin batutunmu na CommonJS kuma ya sake mayar da mu ES6 da kuma React JSX a cikin Javascript mai jituwa-bincike
Codmic JS Jami'ar Node. js abokin ciniki don sauƙaƙƙin hidima ta yanar gizo daga Cosmic JS girgije-hosted abun ciki API
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}}
Sa'an nan kuma kewaya zuwa http: // localhost: 8080 a cikin burauzarku don ganin rubutunku na asali a cikin aiki.
Anyi waɗannan abubuwa, yanzu ne yanzu don samun wannan don gudu akan uwar garke. Ƙirƙiri fayil da aka kira app-server. js kuma ƙara wannan abun ciki:
// 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   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 ()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'))
A cikin app-server. js , 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.
Shirya shirye don fara uwar garke kuma duba lambarmu akan shi, amma na farko, bari mu kirkiri rubutun don yin haka.
Buɗe kunshin. json fayil kuma shirya sashen rubutun don kama da wadannan:
// ."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"},// .
Yanzu da muna da samar rubutun kafa, za mu iya tafiyar da lambarmu a kan sassan uwar garken da abokin ciniki. A cikin mota kashe:
npm farawa
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.
Ku ci gaba da buga duba mabulin . Yi la'akari da lambar SPA tana samuwa ga dukkan 'yan fashi. Muna samun mafi kyawun dukansu biyu!
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.
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 Flux pattern .
Mun haɗu tare da Open SourceCraft don kawo maka 6 Pro Tips daga Masu Rikici Aiki . 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.