首页 技术 正文
技术 2022年11月8日
0 收藏 557 点赞 1,165 浏览 2453 个字

在 node.js 中模块分为核心模块和文件模块两种,核心模块是通过 require(‘xxxx’) 导入的,文件模块是以 require(‘/xxxx’) 或 require(‘./xxxx’)、require(‘../xxxx’) 形式导入的;核心模块是用c/c++编译的二进制模块,而文件模块是后缀为.js、.json、.node 的文件,在 node.js 中一个文件/文件夹也可以称之为一个模块。更多关于模块及模块加载顺序的信息请查阅官网:http://nodejs.org/api/all.html#all_modules 
这里导入了 express、http、path 核心模块,routes 文件夹下的 index.js 和 user.js 文件模块。

因为 express 框架是依赖 connect 框架(Node的一个中间件框架)创建而成的,可查阅 connect 文档:http://www.senchalabs.org/connect/和 express 官方文档:http://expressjs.com/api.html了解更多内容。

app.set(name, value):设置 name 的值为 value

app.set(‘port’, process.env.PORT || 3000):设置端口为 process.env.PORT 或 3000

app.set(‘views’, __dirname + ‘/views’):设置 views 文件夹为视图文件的目录,存放模板文件,__dirname 为全局变量,存储着当前正在执行脚本所在的目录名。

app.set(‘view engine’, ‘ejs’):设置视图模版引擎为 ejs

app.use([path], function):使用中间件 function,可选参数path默认为”/”

app.use(express.favicon()):connect 内建的中间件,使用默认的 favicon 图标,如果想使用自己的图标,需改为app.use(express.favicon(__dirname + '/public/images/favicon.ico')); 这里我们把自定义的 favicon.ico 放到了 public/images 文件夹下。

app.use(express.logger(‘dev’)):connect 内建的中间件,在开发环境下使用,在终端显示简单的不同颜色的日志,比如在启动 app.js 后访问 localhost:3000,终端会输出:

Express server listening on port 3000 GET / 200 21ms - 206b GET /stylesheets/style.css 304 4ms 

数字200显示为绿色,304显示为蓝色。假如你去掉这一行代码,不管你怎么刷新网页,终端都只有一行 Express server listening on port 3000。

app.use(express.bodyParser()):connect 内建的中间件,用来解析请求体,支持 application/json, application/x-www-form-urlencoded, 和 multipart/form-data。

app.use(express.methodOverride()):connect 内建的中间件,可以协助处理 POST 请求,伪装 PUT、DELETE 和其他 HTTP 方法。

app.use(app.router):设置应用的路由(可选),详细请参考:http://stackoverflow.com/questions/12695591/node-js-express-js-how-does-app-router-work

app.use(express.static(path.join(__dirname, ‘public’))):connect 内建的中间件,设置根目录下的 public 文件夹为静态文件服务器,存放 image、css、js 文件于此。

if (‘development’ == app.get(‘env’)) {app.use(express.errorHandler());}:开发环境下的错误处理,输出错误信息。

app.get(‘/’, routes.index):路由控制器,如果用户访问” / “路径,则由 routes.index 来控制,routes/index.js 内容如下:

exports.index = function(req, res){ res.render('index', { title: 'Express' }); }; 

通过 exports.index 导出 index 函数接口,app.get('/', routes.index) 相当于:

app.get('/', function(req, res){ res.render('index', { title: 'Express' }); };) 

res.render(‘index’, { title: ‘Express’ }):调用 ejs 模板引擎解析 views/index.ejs(我们之前通过 app.set('views', __dirname + '/views')设置了模版文件默认存储在 views 下),并传入一个对象作为参数,这个对象只有一个属性 title: ‘Express’,即用字符串 Express 替换 views/index.ejs 中所有 title 变量,后面我们将会了解更多关于模板引的内容。

http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); 

这段代码的意思是创建服务器并监听3000端口,成功后在命令行中显示 Express server listening on port 3000,然后我们就可以通过在浏览器输入 localhost:3000 来访问了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295