本文以个人阅读实践经验归纳前端架构构建过程,以Step by Step方式说明创建一个前端项目的过程。并会对每个阶段所使用的技术进行可替代分析,如Express替换Hapi或者Koa的优缺点分析。本文仅供参考。

流程

1. Package.json

首先,我们需要创建package.json文件。对设计初期已知的引用包和依赖包进行管理,使用ES6的,需要设置babel。其次编写脚本命令。一般文件形式如下:

{
  "name": "practice",
  "description": "Ryan Project",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "watch": "nodemon server.js"
  },
  "babel": {
    "presets": [
      "es2015",
      "react"
    ]
  },
  "dependencies": {
    "alt": "^0.17.8",
    "async": "^1.5.0",
    "body-parser": "^1.14.1",
    "colors": "^1.1.2",
    "compression": "^1.6.0",
    "express": "^4.13.3",
    "history": "^1.13.0",
    "mongoose": "^4.2.5",
    "morgan": "^1.6.1",
    "react": "latest",
    "react-dom": "latest",
    "react-highcharts": "^10.0.0",
    "react-router": "^1.0.0",
    "request": "^2.65.0",
    "serve-favicon": "^2.3.0",
    "socket.io": "^1.3.7",
    "swig": "^1.4.2",
    "underscore": "^1.8.3",
    "xml2js": "^0.4.15"
  },
  "devDependencies": {
    "babel-core": "^6.1.19",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "babel-register": "^6.3.13",
    "babelify": "^7.2.0",
    "bower": "^1.6.5",
    "browserify": "^12.0.1",
    "gulp": "^3.9.0",
    "gulp-autoprefixer": "^3.1.0",
    "gulp-concat": "^2.6.0",
    "gulp-cssmin": "^0.1.7",
    "gulp-if": "^2.0.0",
    "gulp-less": "^3.0.3",
    "gulp-plumber": "^1.0.1",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-uglify": "^1.4.2",
    "gulp-util": "^3.0.7",
    "optimize-js": "^1.0.0",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0",
    "watchify": "^3.6.0"
  },
  "license": "MIT"
}

输入完成后,运行npm install,将package.json中的包安装到项目目录中,存放于对应node_modules文件夹

2. Server.js

即服务端,可以使用Express、Koa、Hapi等方式去创建服务端,设置服务端口。也可以设置socket相关的工作。

Express创建服务端

var express = require('express'); 
var app = express();

//创建路由
app.get('/', function(req, res) {
  res.send('Hello world');
});

//创建REST API
var router = express.Router();
router.route('/items/:id')
.get(function(req, res, next) {
  res.send('Get id: ' + req.params.id); })
.put(function(req, res, next) {
  res.send('Put id: ' + req.params.id); })
.delete(function(req, res, next) {   
  res.send('Delete id: ' + req.params.id); });
app.use('/api', router);
var server = app.listen(3000, function() {
  console.log('Express is listening to http://localhost:3000');
});

Koa创建服务端

var koa = require('koa');
var app = koa();
//创建路由
app.use(function *() {
  this.body = 'Hello world';
});
//创建REST API
app.use(route.get('/api/items', function*() { this.body = 'Get'; }));
app.use(route.post('/api/items', function*() { this.body = 'Post'; }));
app.use(route.put('/api/items/:id', function*(id) { this.body = 'Put id: ' + id; }));
app.use(route.delete('/api/items/:id', function*(id) { this.body = 'Delete id: ' + id; }));
var server = app.listen(3000, function() { console.log('Koa is listening to http://localhost:3000'); });

Hapi创建服务端

var Hapi = require('hapi');
var server = new Hapi.Server(3000);
server.route({ 
  method: 'GET',
  path: '/',
  handler: function(request, reply) {
    reply('Hello world'); } });
server.route([
  { method: 'GET', path: '/api/items', handler: function(request, reply) { reply('Get item id'); } },
  { method: 'GET', path: '/api/items/{id}', handler: function(request, reply) { reply('Get item id: ' + request.params.id); } },
  { method: 'POST', path: '/api/items', handler: function(request, reply) { reply('Post item'); } },
  { method: 'PUT', path: '/api/items/{id}', handler: function(request, reply) { reply('Put item id: ' + request.params.id); } },
  { method: 'DELETE', path: '/api/items/{id}', handler: function(request, reply) { reply('Delete item id: ' + request.params.id); } },
  { method: 'GET', path: '/', handler: function(request, reply) { reply('Hello world'); } } ]);
server.start(
function() { console.log('Hapi is listening to http://localhost:3000'); });

三者间优缺点比较

  优点 缺点
Express 庞大的社区,相对成熟。极易方便创建服务端,创建路由方面代码复用率高 基于callback机制,不可以组合使用,也不能捕获异常
Koa

相比Express,移除Route和View,中间件的使用移植和编写都比较方便,拥抱ES6,

借助Promise和generator而非callback,能够捕获异常和组合使用

以Express一样,需要routers中间件处理不同的选择
Hapi  基于配置而非代码的框架,对于大型项目的一致性和可重用性比较有用。 为大型项目定制,导致在小项目中,常见的过于形式化的代码。相关的开源资料也比较少

 

3. 工程化工具

首先,我们需要先设计好我们项目的目录结构,以便使用工程化工作进行压缩打包等操作。

简单举例如下项目的结构

--/public
--/css
--/js
--/fonts
--/img
--/app
    --/actions
    --/components
    --/stores
    --/stylesheets
        --main.less
    --alt.js
    --route.js
    --main.js

其次,需要webpack或者browserify工具,打包压缩一系列的脚本文件。使用babel转换ES6语法,因为绝大部分的浏览器还不支持ES6,所以需要转换为ES5。最后,创建gulpfile.js文件,使用gulp创建系列的工程指令,如绑定vendor文件、引用sourcemap、使用类似uglify、gulp-cssmin等辅助压缩文件。

如下是简易的gulpfile.js文件的配置

var gulp = require('gulp');
var gutil = require('gulp-util');
var gulpif = require('gulp-if'); //conditionally run a task
var autoprefixer = require('gulp-autoprefixer'); //Prefix CSS
var cssmin = require('gulp-cssmin');
var less = require('gulp-less'); //Less for Gulp
var concat = require('gulp-concat');
var plumber = require('gulp-plumber'); //Prevent pipe breaking caused by errors from gulp plugins
var buffer = require('vinyl-buffer'); //convert streaming vinyl files to use buffers
var source = require('vinyl-source-stream'); //Use conventional text streams at the start of your gulp or vinyl pipelines
var babelify = require('babelify');
var browserify = require('browserify');
var watchify = require('watchify');
var uglify = require('gulp-uglify'); //Minify files with UglifyJS.
var sourcemaps = require('gulp-sourcemaps');

var production = process.env.NODE_ENV === 'production'

var dependencies = [
    'alt',
    'react',
    'react-dom',
    'react-router',
    'underscore'
]
/*
 |--------------------------------------------------------------------------
 | Combine all JS libraries into a single file for fewer HTTP requests.
 |--------------------------------------------------------------------------
 */
gulp.task('vendor', function () {
    return gulp.src([
        'bower_components/jquery/dist/jquery.js',
        'bower_components/bootstrap/dist/js/bootstrap.js',
        'bower_components/magnific-popup/dist/jquery.magnific-popup.js',
        'bower_components/toastr/toastr.js'
    ]).pipe(concat('vendor.js'))
    .pipe(gulpif(production, uglify({mangle: false})))
    .pipe(gulp.dest('public/js'))
})
/*
 |--------------------------------------------------------------------------
 | Compile third-party dependencies separately for faster performance.
 |--------------------------------------------------------------------------
 */
gulp.task('browserify-vendor', function(){
    return browserify()
    .require(dependencies)
    .bundle()
    .pipe(source('vendor.bundle.js'))
    .pipe(buffer())
    .pipe(gulpif(production, uglify({mangle: false})))
    .pipe(gulp.dest('public/js'))
})

/*
 |--------------------------------------------------------------------------
 | Compile only project files, excluding all third-party dependencies.
 |--------------------------------------------------------------------------
 */
gulp.task('browserify',['browserify-vendor'], function(){
    return browserify({entries:'app/main.js', debug: true})
    .external(dependencies)
    .transform(babelify, {presets: ['es2015','react']})
    .bundle()
    .pipe(source('bundle.js'))
    .pipe(buffer())
    .pipe(soucemaps.init({loadMaps: true}))
    .pipe(gulpif(production, uglify({mangle: false})))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('public/js'))
})

/*
 |--------------------------------------------------------------------------
 | Same as browserify task, but will also watch for changes and re-compile.
 |--------------------------------------------------------------------------
 */
gulp.task('browserify-watch', ['browserify-vendor'], function(){
    var bundler = watchify(browserify({ entries:'app/main.js', debug: true}), watchify.args)
    bundler.external(dependencies)
    bundler.transform(babelify, {presets: ['es2015', 'react']})
    bundler.on('update', rebundle)
    return rebundle()

    function rebundle() {
        var start = Date.now()
        return bundler.bundle()
        .on('error', function(err){
            gutil.log(gutil.colors.red(err.toString()))
        })
        .on('end', function() {
            gutil.log(gutil.colors.green(`Finished rebundling in ${(Date.now() - start)} ms`))
        })
        .pipe(source('bundle.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('public/js'))
    }
})

gulp.task('styles', function(){
    return gulp.src('app/stylesheets/main.less')
    .pipe(plumber())
    .pipe(less())
    .pipe(autoprefixer())
    .pipe(gulpif(production, cssmin()))
    .pipe(gulp.dest('public/css'))
})

gulp.task('watch', function(){
    gulp.watch('app/stylesheets/**/*.less', ['styles'])
})

gulp.task('default', ['styles','vendor','browserify-watch','watch'])
gulp.task('build', ['styles', 'vendor', 'browserify'])
View Code

相关文章:

  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2021-12-11
  • 2022-12-23
  • 2021-07-25
  • 2021-12-04
猜你喜欢
  • 2022-12-23
  • 2021-04-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2021-07-15
相关资源
相似解决方案