学习一门语言,我们先要学习他的基本的语法,基本的数据类型,基本的数组操作,字符串的操作,然后就是语言的特性,实现共享和降低耦合的方式,然后开始比较高级的学习(所有语言都是一样的),比如说通信方法,tcp http等,io的操作,多进程,多线程的通信方式,阻塞非阻塞,对数据库的操作,性能的提升和更好的设计模式架构等。

当然对于一些tomcat,nginx,pm2,对服务器和一些服务器相关的工具的熟练使用,可能比上面的基础还要重要。

我们学习Node这门服务端的语言,同样。学习他后台的框架 express和koa还有基于express的hapi等,了解他的更多的核心包,还有很多基于Node开发的前端Mvc的框架等,尝试使用,毕竟一个公司通常用的东西都是成熟稳定的东西,新的东西风险太大。

尝试用前后端分离的mokjs,framejs,以前前端模块化的一些东西等。

对前端优化的工具gulp等有比较好的使用。

我们对node的学习,对基本库的使用,对数据库的操作,对前台的数据返回,对session,cookie等的设置,与前台的数据交互方式,对于文件的上传,读写等。

不断的积累源码,不断的提升自己。

******************************************************

node的核心包:

******************************************************

1. 问题:如何遍历配置中的包,确定所有的都加载了。

//检查下依赖的模块是否都已安装
(function() {
var errors = [];
var packages = require('../package.json');
Object.keys(packages.dependencies).forEach(function (p) {
try {
require.resolve(p);
} catch (e) {
errors.push(e.message);
}
}); if (!errors.length) {
return;
}
errors = errors.join('\n ');
console.error('\x1B[31mERROR: creative platform is unable to start due to missing dependencies:\x1B[0m\n ' + errors);
console.error('\x1B[32m\nPlease run `npm install` and try starting creative platform again.');
console.error('\x1B[32mHelp and documentation can be found at http://wiki.letv.cn/pages/viewpage.action?pageId=46202060\x1B[0m\n');
process.exit();
}());

后面的console.error中的  \x1B[31m  表示字体的颜色和字体的大小。

2.node中的绝对路径怎么搞:__dirname

在任何模块文件内部,可以使用__dirname变量获取当前模块文件所在目录的完整绝对路径

3.path.resolve方法的使用。http://www.jb51.net/article/58295.htm

contentPath = path.resolve(__dirname, '../../content');

4.require的方法的使用。http://www.ruanyifeng.com/blog/2015/05/require.html

5.检查某些目录是否存在。

对Promise的使用。

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve

//检查下content目录是否存在
var checkContent = new Promise(function(resolve, reject) {
var stats;
try {
//服务器启动前,可执行同步io
stats = fs.statSync(appConfig.contentPath);
} catch (e) {
if (e.code == 'ENOENT') {
return resolve(false);
} else {
return reject(e);
}
}
if (stats.isDirectory()) {
return resolve(true);
} else {
return reject(new Error(appConfig.contentPath + ' is not a directory'));
}
});
Promise.resolve(checkContent).then(function(exist) {
var promise;
if (!exist) {
//没有content目录时,在开发模式下,才会创建
if (appConfig.mode === appConfig.DEVELOPMENT) {
var ncpAsync = Promise.promisify(ncp);
promise = ncpAsync(appConfig.contentExamplePath, appConfig.contentPath);
} else {
promise = Promise.reject(new Error('Cannot find directory ' + appConfig.contentPath));
}
} else {
promise = Promise.resolve();
}
return promise;
}).then(function() {
//检查下服务器上是否有flash的编译器
return new Promise(function(resolve, reject) {
var stats;
try {
//服务器启动前,可执行同步io
stats = fs.statSync(path.join(appConfig.env.flexSDKPath, 'bin'));
} catch(e) {
return reject(e);
}
if (!stats.isDirectory()) {
return reject(new Error('Cannot found flex sdk'));
}
return resolve();
});
}).then

6. process模块    http://www.css88.com/archives/4548

7. 一个比较完整的express的配置

var app      = express();
var rootPath = process.cwd(); var server = {
startup: function() {
return new Promise(function(resolve, reject) {
app.set('views', path.join(rootPath, 'views'));
app.set('view engine', 'ejs'); if (appConfig.mode !== appConfig.DEVELOPMENT) {
app.set('trust proxy', );
} app.use(favicon(path.join(rootPath, 'public/images/favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser()); var options = {
resave : true,
saveUninitialized : true,
secret : appConfig.env.sessionSecret,
rolling : true,
cookie : {
maxAge : appConfig.env.sessionMaxAge
}
}; if (appConfig.mode !== appConfig.DEVELOPMENT) {
//将session写到redis中
options.store = new RedisStore( {retry_max_delay: , max_attempts: } );
} //flashSession用来修复flash上传组件上传文件时丢失cookie的bug
app.use(flashSession());
app.use(session(options)); if (appConfig.mode === appConfig.DEVELOPMENT) {
//显示静态资源列表(只显示public目录下的,并不是所有的)
app.use('/static', serveIndex(path.join(rootPath, 'public'), {
icons : true,
hidden : true,
view : 'details'
}));
} app.use(express.static(path.join(rootPath, 'public'))); //编译生成的swf文件及上传的文件的静态资源目录
app.use(express.static(appConfig.staticDir.assetsPath)); app.use(express.static(appConfig.staticDir.apsPath)); //刊例和素材规范的静态资源目录
app.use(express.static(appConfig.staticDir.ratecardPath)); //刊例和素材规范相关的图片, js, 样式
app.use('/rc', express.static(path.join(rootPath, 'public'))); //controller作为中央控制器,集中管理路由
controller.route(app); app.listen(appConfig.env.port, '0.0.0.0', , function(err) {
console.log('\n');
console.log(' fttt8 GCCCG C1t10 ');
console.log(' fttt8 0f11111tG 0CCftttLCLG 0GL0 8CCL0 ');
console.log(' fttt8 0tttC00Gf10 C11tttt1110 LC1L G1t1G ');
console.log(' fttt8 fttG GtfG CCftttLCC0G G1f Cttt8 ');
console.log(' fttt8 0ttf 8GLttt0 L1t10 0tt8 fttf ');
console.log(' fttt8 CttfCft11fC8 L1t10 8f1G 0tttL ');
console.log(' fttt8 C1tt11fL08 L1t10 L1L Gttt0 ');
console.log(' fttt8 GtttL08 8 L1t10 C1f Lttt8 ');
console.log(' fttt8 8tttG C0 L1t10 0ttLtttL ');
console.log(' ftttG00008 C1ttC8 80fL LtttC00 8ttttttG ');
console.log(' fttttttt1f L1tttft1f8 L1ttttt0 L1tttt8 ');
console.log(' LtfffffftL GfttttC Gtfffff0 GtfftL ');
console.log(' 800 ');
console.log(' ');
console.log(' Creative platform ' + pack.version );
console.log(' ');
console.log(' Server running at ' + appConfig.env.httpServerURL );
console.log('');
console.log('\x1B[32m\nHelp and documentation can be found at http://wiki.letv.cn/pages/viewpage.action?pageId=46202060\x1B[0m\n');
return resolve();
});
});
}
}; module.exports = server;

6.

var appConfig      = require('../config/app_config');
var ErrorCode = require('../models/error_code'); function Controller() {} /**
* 集中管理路由
* @method route
* @for Controller
* @param {Function} app 应用程序
*/
Controller.prototype.route = function(app) {
var check_login_cmd = require('../commands/auth/check_login_cmd');
var check_logout_cmd = require('../commands/auth/check_logout_cmd');
var index_cmd = require('../commands/index_cmd');
var login_cmd = require('../commands/login_cmd'); var creative = require('../commands/creative'); //广告创意
var user = require('../commands/user'); //用户
var admin = require('../commands/admin');
var ratecard = require('../commands/ratecard'); //刊例
var post = require('../commands/post'); //发表文章
var spec = require('../commands/spec'); //素材规范
var ueditor = require('../commands/ueditor'); //百度编辑器
var openapi = require('../commands/openapi'); //开放平台 this.app = app; app.route('/').get(check_login_cmd('html'), index_cmd);
app.route('/login').get(check_logout_cmd('html'), login_cmd); //将/creative/下的请求都交给creative,即交给creative目录下的index.js
//来处理,index.js控制二级路由,/user, /ratecard等以此类推
app.use('/creative', creative);
app.use('/user', user);
app.use('/admin', admin);
app.use('/ratecard', ratecard);
app.use('/post', post);
app.use('/spec', spec);
app.use('/ueditor', ueditor);
app.use('/openapi', openapi); //
app.use(function(req, res, next) {
res.status();
res.render('', {
message: req.url + ' not found'
});
}); //服务器错误
app.use(function(err, req, res, next) {
res.status(err.status || );
res.render('error', {
error: appConfig.mode == appConfig.PRODUCTION ? {stack: ''} : err
});
});
}; Controller.prototype.responseJson = function(res, data, msg, errCode) {
if(errCode === true) {
errCode = ErrorCode.ERROR;
}else if(errCode === false) {
errCode = ErrorCode.SUCCESS;
}
var jsonData = {};
jsonData.data = data;
jsonData.msg = msg,
jsonData.error = errCode;
var head = {
'Content-Type': 'application/json',
};
res.writeHead(, head);
res.end(JSON.stringify(jsonData));
}; Controller.prototype.responseJsonObj = function(res, data) {
var head = {
'Content-Type': 'application/json',
};
res.writeHead(, head);
res.end(JSON.stringify(data));
}; Controller.prototype.responseTxt = function(res, txt) {
res.end(txt);
}; Controller.prototype.response404 = function() {
res.status();
res.render('', {
message: req.url + ' not found...'
});
}; Controller.prototype.responseHtml = function() {
var res, options, path, data;
if(arguments.length == ) {
res = arguments[];
options = arguments[];
path = arguments[];
data = arguments[];
}else if(arguments.length == ) {
res = arguments[];
path = arguments[];
data = arguments[];
}else if(arguments.length == ) {
res = arguments[];
path = arguments[];
}
if(!options) {
options = {};
}
if(options.noCache) {
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Pragma', 'no-cache');
}
if(options.viewsPath) {
var app = this.app;
var viewsPath = app.get('views');
app.set('views', options.viewsPath);
res.render(path, data || {});
app.set('views', viewsPath);
}else {
res.render(path, data || {});
}
}; Controller.prototype.responseFile = function(res, statusCode, head, file) {
res.writeHead(statusCode, head);
res.write(file, "binary");
res.end();
}; Controller.prototype.redirect = function(res, path) {
res.redirect(path);
}; var controller = new Controller();
module.exports = controller;

node的实践(项目一)的更多相关文章

  1. 8 步搭建 Node.js + MongoDB 项目的自动化持续集成

    任何事情超过 90 秒就应该自动化,这是程序员的终极打开方式.Automating shapes smarter future. 这篇文章中,我们通过创建一个 Node.js + MongoDB 项目 ...

  2. React 实践项目 (二)

    React在Github上已经有接近70000的 star 数了,是目前最热门的前端框架.而我学习React也有一段时间了,现在就开始用 React+Redux 进行实战! React 实践项目 (一 ...

  3. python编程快速上手之第10章实践项目参考答案

      本章主要讲了python程序的调试,当程序有BUG或异常的时候,我们如何调试代码找出问题点.其实在本章之前的章节我们做练习的时候都会遇到各种各样的错语和异常,最初当不知道程序哪里出错的情况下不可否 ...

  4. python编程快速上手之第9章实践项目参考答案

    本章介介绍了shutil,zipfile模块的使用,我们先来认识一下这2个模块吧. 一.shutil模块 shutil模块主要用于对文件或文件夹进行处理,包括:复制,移动,改名和删除文件,在shuti ...

  5. python编程快速上手之第8章实践项目参考答案

    第8章实践项目之疯狂填词 创建一个一个疯狂填词(Mad Libs),程序,它将读入文本文件,并让用户在该文本文件中出现 ADJECTIVE,NOUN,VERB等单词的地方,加上他们自己的文本. 首先准 ...

  6. React 实践项目 (三)

    React在Github上已经有接近70000的 star 数了,是目前最热门的前端框架.而我学习React也有一段时间了,现在就开始用 React+Redux 进行实战! 上回说到使用Redux进行 ...

  7. React 实践项目 (五)

    React在Github上已经有接近70000的 star 数了,是目前最热门的前端框架.而我学习React也有一段时间了,现在就开始用 React+Redux 进行实战! React 实践项目 (一 ...

  8. iKcamp团队制作|基于Koa2搭建Node.js实战项目教学(含视频)☞ 环境准备

    安装搭建项目的开发环境 视频地址:https://www.cctalk.com/v/15114357764004 文章 Koa 起手 - 环境准备 由于 koa2 已经开始使用 async/await ...

  9. 关于Prometheus运维实践项目

    关于Promethues运维实践项目 1. 什么是Prometheus运维实践项目 ​ 是什么 ​ Prometheus,普罗米修斯,是古希腊神话中为人间带来火种的神. ​ Prometheus运维实 ...

  10. 《Java 程序设计》课堂实践项目 课后学习总结

    <Java 程序设计>课堂实践项目 课后学习总结 String类的使用(sort) 目录 Linux命令(sort) 课堂实践 课后思考 学习老师的代码之后的思考:int与Integer ...

随机推荐

  1. python 练习多级菜单思路

    只写了一个zj的三级菜单,后面的功能没写 #-*- coding :utf-8 -*- print """ 你可以输入省份然后根据市县输入 ""&qu ...

  2. Error during installing HAXM, VT-X not working 在安装HAXM错误,开始不工作

    最佳答案 (Best Answer) Some antivirus options prevent Haxm installation. ie: Avast : settings (parametre ...

  3. Oracle Stored Procedure demo

    1.how to find invalid status stored procedure and recompile them? SELECT OBJECT_NAME , status FROM u ...

  4. Memcached内存分配优化及使用问题

    前几天做了个Memcached的思考,并测试了一些数据,是关于如何提高Memcached内存使用率的问题.在启动memcached的时候可以加-f参数和-n参数.-f指定各slab里面chunk大小的 ...

  5. Java基本运算符

    1.算术运算符 作用是数字的计算,包括:正号+,负号-,乘*,除/,余%,加+,减-,其算法与数学中的运算相同. 算术运算符实例(假设变量A=10,变量B=20): 操作符 描述 例子 + 正号 +A ...

  6. 对访问修饰关键字public, protected, internal and private的说明

    对访问修饰关键字public, protected, internal and private的说明1.msdn: Internal types or members are accessible o ...

  7. 过滤掉combobox里名称相同的选项

    var pname = ""; $('#PartName').combobox({ reload: url, formatter: function (row) {//过滤comb ...

  8. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter与org.apache.struts.dispatcher.FilterDispatcher是什么区别?

    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter与org.apache.struts.dispatcher.F ...

  9. CSS选择器的权重与优先规则

    权重顺序 “important > 内联 > ID > 类 > 标签 | 伪类 | 属性选择 > 伪对象 > 继承 > 通配符”.   原文:http://w ...

  10. Android动画原理分析

    最近在Android上做了一些动画效果,网上查了一些资料,有各种各样的使用方式,于是乘热打铁,想具体分析一下动画是如何实现的,Animation, Animator都有哪些区别等等. 首先说Anima ...