NodeJs(三)

一、module.exports 和 exports的区别

1、关系
对于希望被其他成员访问的成员,我们需要把这些公开的成员都挂载到exports接口对象中就可以导出多个成员

在node中每个模块内部都有自己的module对象,该module对象中有个成员叫:exports也是一个对象

var module = {
   exports:{
       
    }
}

默认在代码的最后一行有:return module.exports,因此最终导出的是module.exports,但是因为每次导出都这样写会比较麻烦,因此可以直接写exports。
也就相当于:exports = module.exports

2、导出成员
实际导出成员的时候:

  • 导出多个成员:exports.xxx = xxx
  • 导出多个成员也可以:module.exports = { a:xxx, b:xxx }
  • 导出单个成员:module.exports = ‘hello’

二、修改完代码自动重启

nodemon :是一个基于 node.js 开发的一个第三方命名行工具,使用的时候需要独立全局安装

npm install --global nodemon

安装完之后,就可以使用nodemon来启动服务器了,只要是通过 nodemon 启动的服务,它会监视你的文件变化,当文件发生变化的时候,自动帮你重启服务器

三、Express


1、简介

原生的http在某些方面不足以满足我们的需求,因此使用框架,使我们的代码统一。

Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具。使用 Express 可以快速地搭建一个完整功能的网站。

Express 框架核心特性:

  • 可以设置中间件来响应 HTTP 请求。
  • 定义了路由表用于执行不同的 HTTP 请求动作。
  • 可以通过向模板传递参数来动态渲染 HTML 页面。

2、安装 Express

  • 全局安装express模块
    npm install express -g

-g 等同于 -global,表示将express模块安装到全局,否则会安装到当前所在的文件夹下,建议使用全局模式,方便管理模块。
注意:现在安装的都是最新版4.0+,会导致express命令失效,所以要同步安装express命令行工具
npm install -g express-generator

安装完后执行express -v,看到如下界面就说明安装成功
在这里插入图片描述

  • 局部安装
    npm i -S express

这个命令会将 Express 框架安装在当前目录的 node_modules 目录中, node_modules 目录下会自动创建 express 目录。

3、初始化项目


在这里插入图片描述

4、 Express 实例

//1.引包
var express = require('express');
//2.创建服务器应用程序,也就是原来的http.creatServer
var app = express()
//当服务器收到get请求/的时候,执行回调处理函数
app.get('/',function(req,res){
    res.end('hello express')
})
//相当于server.listen
app.listen(50,function(){
    console.log('run~');
})

在框架中不用去设置content-type,express会自动为你设置

5、在express中的静态资源服务

让app.js能够访问到public中的文件

在这里插入图片描述

//当以 /public/ 开头的时候,去 ./public/ 文件夹中查找对应的资源
//这种方式更容易辨识,推荐这种方式
 app.use('/public/', express.static('./public/'))
 这种方法请求的地址:http://localhost:50/public/a.html

// 必须是 /a/puiblic目录中的资源具体路径
// app.use('/abc/', express.static('./public/'))
 这种方法请求的地址:http://localhost:50/abc/a.html

// 当省略第一个参数的时候,则可以通过 省略 /public 的方式来访问
// 这种方式的好处就是可以省略 /public/
app.use(express.static('./public/'))
这种方法请求的地址:http://localhost:50/a.html

6、在express中配置使用art-template

  • 安装
    npm install --save art-template
    npm install --save express-art-template

    express-art-template 是专门用来在 Express 中把 art-template 整合到 Express 中
    虽然外面这里不需要加载,但是也必须安装

  • 配置和使用

// 第一个参数,表示,当渲染以 .art 结尾的文件的时候,使用 art-template 模板引擎
// 原因就在于 express-art-template 依赖了 art-template
app.engine('art', require('express-art-template'))

// Express 为 Response 相应对象提供了一个方法:render
// render 方法默认是不可以使用,但是如果配置了模板引擎就可以使用了
// res.render('html模板名', {模板数据})
// 第一个参数不能写路径,默认会去项目中的 views 目录查找该模板文件
app.get('/',function(req,res){
  res.render('index.art',{
        title:"hello"
    }
  )
})

// 如果想要修改默认的 views 目录,则可以
// app.set('views', render函数的默认路径)

7、留言板案例

这里只放了app.js中的文件!

//1.引包
var express = require('express');
//2.创建服务器应用程序,也就是原来的http.creatServer
var app = express()

// app.use('/public/', express.static('./public/'))

app.engine('html', require('express-art-template'))
var comments = [{
    name: '张三',
    message: '今天天气不错!',
    dateTime: '2020-10-11  15:58:51'
  },
  {
    name: '李四',
    message: '今天天气不错!',
    dateTime: '2020-10-10  10:08:51'
  },
  {
    name: '王五',
    message: '今天天气不错!',
    dateTime: '2020-10-9  15:58:51'
  },
  {
    name: '李六',
    message: '今天天气不错!',
    dateTime: '2020-10-8  15:58:51'
  }
]

app.get('/', function (req, res) {
  res.render('index.html', {
    comments: comments
  })
})

app.get('/post', function (req, res) {
  res.render('post.html')
})
app.get('/pinglun', function (req, res) {
  var comment = req.query
  comment.dateTime = '2020-10-12 15:58:51'
  comments.unshift(comment)
  res.redirect('/')
})
app.listen(50, function () {
  console.log('running...')
})

结果展示:

  • 进入页面在这里插入图片描述
  • 点击发表留言之后跳转
    在这里插入图片描述
  • 点击发表之后跳转回到首页
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现级联动的方式有很多种,以下是一种基本的实现方式: 1. 定义个下拉框。 ```html <select id="firstSelect"></select> <select id="secondSelect"></select> <select id="thirdSelect"></select> ``` 2. 获取数据并初始化第一个下拉框。 ```javascript const data = [ { id: 1, name: '北京市', children: [ { id: 11, name: '东城区', children: [ { id: 111, name: 'xxx街道' }, { id: 112, name: 'yyy街道' }, { id: 113, name: 'zzz街道' }, ]}, { id: 12, name: '西城区', children: [ { id: 121, name: 'aaa街道' }, { id: 122, name: 'bbb街道' }, { id: 123, name: 'ccc街道' }, ]}, ]}, { id: 2, name: '上海市', children: [ { id: 21, name: '黄浦区', children: [ { id: 211, name: 'ddd街道' }, { id: 212, name: 'eee街道' }, { id: 213, name: 'fff街道' }, ]}, { id: 22, name: '徐汇区', children: [ { id: 221, name: 'ggg街道' }, { id: 222, name: 'hhh街道' }, { id: 223, name: 'iii街道' }, ]}, ]}, ]; const firstSelect = document.getElementById('firstSelect'); data.forEach(item => { const option = document.createElement('option'); option.value = item.id; option.text = item.name; firstSelect.appendChild(option); }); ``` 3. 根据一级选项的值初始化第二个下拉框。 ```javascript const secondSelect = document.getElementById('secondSelect'); const thirdSelect = document.getElementById('thirdSelect'); firstSelect.addEventListener('change', function() { const firstValue = this.value; const firstItem = data.find(item => item.id === Number(firstValue)); secondSelect.innerHTML = ''; thirdSelect.innerHTML = ''; if (firstItem && firstItem.children) { firstItem.children.forEach(item => { const option = document.createElement('option'); option.value = item.id; option.text = item.name; secondSelect.appendChild(option); }); } }); ``` 4. 根据二级选项的值初始化第个下拉框。 ```javascript secondSelect.addEventListener('change', function() { const secondValue = this.value; const firstValue = firstSelect.value; const firstItem = data.find(item => item.id === Number(firstValue)); const secondItem = firstItem.children.find(item => item.id === Number(secondValue)); thirdSelect.innerHTML = ''; if (secondItem && secondItem.children) { secondItem.children.forEach(item => { const option = document.createElement('option'); option.value = item.id; option.text = item.name; thirdSelect.appendChild(option); }); } }); ``` 这就是一个简单的级联动实现方式。当然,具体实现还需要根据实际需求做一些调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值