Node.js
一、node.js是什么
二、下载和安装
1.到官网node.js下载
2.傻瓜式安装
3.window和mac
4.打开控制台,运行
5.运行命令:node -v, npm -v,
const http = require('http')
// const a = undefined
// req - request, res - response
const server = http.createServer((req, res) => {
// debugger // 断点
const url = req.url // '/index.html?a=100'
const path = url.split('?')[0] // '/index.html'
// a() // bug
res.end(path + '1234')
})
server.listen(3000)
console.log('server listening on 3000 port')
三、使用npm
const _ = require('lodash')
const arr = [1, 2, 3]
const otherArr = _.concat(arr, 4, 5)
console.log(otherArr) // [1, 2, 3, 4, 5]
Lodash 是一个一致性、模块化、高性能的 JavaScript 实用工具库
nodemon是一种工具,可以自动检测到目录中的文件更改时通过重新启动应用程序来调试基于node.js的应用程序。
npm i nodemon
1、使用命令 --save 或者说不写命令 --save ,都会把信息记录到 dependencies 中;
dependencies 中记录的都是项目在运行时需要的文件;
2、使用命令 --save-dev 则会把信息记录到 devDependencies 中;
devDependencies 中记录的是项目在开发过程中需要使用的一些文件,在项目最终运行时是不需要的;
也就是说我们开发完成后,最终的项目中是不需要这些文件的;
四、common.js
utils.js
function sum (a, b) {
return a + b
}
function test() {
console.log('this is test')
}
module.exports = {
sum,
test
}
index.js
console.log('start...')
const flag = true
if (flag) {
const { sum, test } = require('./utils') // 动态
test()
const res = sum(15, 30)
console.log(res)
}
五、debug
package.json
{
"name": "test1-imooc-demo",
"version": "1.0.0",
"description": "test1 code demo",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon --inspect=9229 index.js"
},
"author": "shuangyue",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.15"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
}
六、nodejs 和 js 区别