Building A Blog By React Ecosystem(1)

Preface

After building a resume generator , I like that feeling build a project that everything's ready for some people to deploy. And then that time I discovered a website could free deploy app with given API manager data, so again I try to build a blog purpose of deploying in minutes for everyone want an own website.

However when I finish my work and after deploying the CMS website sent me an mail to tell me I need to upgrade the CMS account in 14days otherwise they will remove my app. My god, I suddenly more disappointed than any moment I meet difficult problems when I build my app, that faked advertisement destroyed my idea. oh..it just destroyed my recent finished works.

So it seems I built the app but for myself . However, I can share my experiences about I building my project. I believe that also have its valuable .

Main

there is a Chinese saying "A workman must first sharpen his tools if he is to do his work well" , this is the first part of my project: create a building scaffolding .

Before I really show my experience I recommend you to this to read the file architecture of this part.

I think I 'm not necessary to say any more about Nodejs and 'Express' or Webpack right ? After install Webpack globally we can start to write a Webpack.config file. we need ES6 and React .

"devDependencies": {
    "babel-cli": "^6.10.1",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-es2016": "^6.11.0",
    "babel-preset-react": "^6.11.1",
    "eslint": "^3.0.1",
    "eslint-config-airbnb": "^9.0.1",
    "eslint-plugin-import": "^1.9.2",
    "eslint-plugin-jsx-a11y": "^1.5.3",
    "eslint-plugin-react": "^5.2.2",
    "nodemon": "^1.9.2",
    "react-hot-loader": "^1.3.0",
    "webpack-dev-server": "^1.14.1",
    "webpack-hot-middleware": "^2.12.1"
  },
  "dependencies": {
    "babel-core": "^6.10.4",
    "babel-loader": "^6.2.4",
    "babel-polyfill": "^6.9.1",
    "cosmicjs": "^2.1.2",
    "events": "^1.1.1",
    "express": "^4.14.0",
    "history": "^3.0.0",
    "hogan-express": "^0.5.2",
    "immutable": "^3.8.1",
    "react": "^15.2.0",
    "react-dom": "^15.2.1",
    "react-redux": "^4.4.5",
    "react-router": "^2.5.2",
    "react-router-redux": "^4.0.5",
    "react-router-scroll": "^0.2.0",
    "redux": "^3.5.2",
    "redux-immutable": "^3.0.6",
    "redux-saga": "^0.11.0",
    "sanitize.css": "^4.1.0",
    "webpack": "^1.13.1"
  } 

The list above is all needs in this part.

At first, we write the server .

import express from 'express';
import path from 'path';

import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackConfig from '../webpack.config.dev';
const app = express();

const compiler = webpack(webpackConfig);
app.use(webpackMiddleware(compiler, {
  hot: true,
  publicPath: webpackConfig.output.publicPath,
  noInfo: true,
}));
app.use(webpackHotMiddleware(compiler));

app.get('/*', (req, res) => {
  res.sendFile(path.join(__dirname, './index.html'));
});

app.listen(3333, () => console.log('Running on localhost 3333'));

The server is for compile React and ES6 imminently . the Express can work to maintain the states and compile fast without restarting the server. after wrote it down we can't make it work because we use ES6.

To solve this problem we can use babel-node instead of node to start Server.

"scripts": {

  "server": " babel-node server "

},

after Writing the scripts line to package.json we can npm run server to start the server use ES6 . However ,the webpack haven't done . We need to write webpack.config.dev.js like follows .


import path from 'path';
import webpack from 'webpack';

export default {
  devtools: 'eval-source-map',
  entry: [
    'webpack-hot-middleware/client',
    path.join(__dirname, '/client/index.js'),
  ],
  output: {
    path: '/',
    publicPath: '/',
  },
  plugins: [
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        include: path.join(__dirname, 'client'),
        loaders: ['react-hot', 'babel'],
      },
    ],
  },
  resolve: {
    extentions: ['', 'js', 'jsx'],
  },
};

This moment we can't run webpack yet . to touch the index file in ./client.
and write down this :

import React from 'react';

import { render } from 'react-dom';

import App from './components/App';

render(<App />, document.getElementById('app'));

You can see that this file is dependent on another file names ./components/App. So we need to build it :


import React from 'react';

class App extends React.Component { // eslint-disable-line react/prefer-stateless-function
  render() {
    return (
      <h1>Hello my scaffold! ~~</h1>
    );
  }
}

export default App;

It's worth to mentioning why I use the class in App.js, if it 's a pure function we can 't make it updating automatically .Pure function without life cycles.

And now we can make work and you can check localhost: 3333 to see how beautiful the project in the browser. and you can edit files and see it changes without waiting.

This part is over . Thank reading.
If having any problem you can contact me .

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末轮蜕,一起剝皮案震驚了整個濱河市掸绞,隨后出現(xiàn)的幾起案子杖剪,更是在濱河造成了極大的恐慌齐佳,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異障贸,居然都是意外死亡,警方通過查閱死者的電腦和手機吟宦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門篮洁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人殃姓,你說我怎么就攤上這事袁波。” “怎么了辰狡?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵锋叨,是天一觀的道長。 經(jīng)常有香客問我宛篇,道長,這世上最難降的妖魔是什么薄湿? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任叫倍,我火速辦了婚禮,結(jié)果婚禮上豺瘤,老公的妹妹穿的比我還像新娘吆倦。我一直安慰自己,他們只是感情好坐求,可當(dāng)我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布蚕泽。 她就那樣靜靜地躺著,像睡著了一般桥嗤。 火紅的嫁衣襯著肌膚如雪须妻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天泛领,我揣著相機與錄音荒吏,去河邊找鬼。 笑死渊鞋,一個胖子當(dāng)著我的面吹牛绰更,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锡宋,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼儡湾,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了执俩?” 一聲冷哼從身側(cè)響起徐钠,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤堰酿,失蹤者是張志新(化名)和其女友劉穎抵蚊,沒想到半個月后载萌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體搪哪,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年摊崭,在試婚紗的時候發(fā)現(xiàn)自己被綠了讼油。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡呢簸,死狀恐怖矮台,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情根时,我是刑警寧澤瘦赫,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站蛤迎,受9級特大地震影響确虱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜替裆,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一校辩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辆童,春花似錦宜咒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至庭砍,卻和暖如春场晶,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背逗威。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工峰搪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人凯旭。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓概耻,卻偏偏與公主長得像,于是被迫代替她去往敵國和親罐呼。 傳聞我的和親對象是個殘疾皇子鞠柄,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容