引用module
比如有math.js
app.js里面可以写
const math = require('./math');//需要路径找到文件
如果math.js里面没有module.exports="hello" 这个 输出的是空的,不是我们想要的,有就输出hello 也能直接用exports
math.js
const add = (x, y) => x+y;
const PI =3.141592;
exports.add = add;
exports.PI = PI;
app.js
const math = require('./math');
console.log(math.PI)
requiring a directory
NPM(node package manager)
NPM is really two things:
a library of thousands of packages publishes by other developers that we can use for free!
a command line tool to easily install and manage those packages in our Node projects
npm install give-me-a-joke
const jokes = require('give-me-a-joke')
npm install colors
安装了后目标目录会有一个文件夹 node_modules
adding global packages
先安装cowsay
npm link cowsay
然后js文件里的require(“cowsay”)就能找到目标了
the all-improtant package.json
mkdir Artster
ls
npm init// make a package.json
会让填写name version main“index.js” author等信息
然后确认后生成了package.json
json里的dependencies会记录安装了什么其他包
installing all dependencies for a project
注意node.modules文件不需要上传github
当在github上看到的时候,一般会有package.json文件。此时clone下来直接node .js会找不到文件
只需要npm install命令 系统就会自动找json文件 安装所有dependencies
language guesser challenge
猜语言游戏
npm i franc
GitHub - wooorm/franc: Natural language detection
const franc = requite("franc");
const langs = require("langs");
const colors = require("colors");
const input = process.argv[2];
const langCode = franc(input);
if(langCode == 'und') {
console.log("sorry could not figure");
}else{
const language = langs.where("3",langCode);
concole.log(`our best guess is:${language.name}`.green)
section 33 creating servers with Express
Express is a framework for creating servers, using nodes
Express is a fast unopinionated, minimalist web framework for Node.js it helps us build web apps!
It is just an NPM package which comes with a bunch of methods and optional plugins that we can use to build web applications and APIs
Express helps us start up a server to listen for requests
parse incoming requests
match those requests to particular routes
craft our http response and associated content
什么事libraries和frameworks
library when you use a library, you are in charge. you control the dlow of the application code and you decide when to use the library
framework:
with frameworks, the control is inverted the framework is in charge, and you are merely a participant! the framework tells you where to plug the code.
步骤
mkdir FirstApp
cd FirstApp
ls
npm init -y
npm i express
node index.js
index.js
const express = require("express");
const app = express()
//console.dir(app)
app.use(() => {
console.log("we get the request")
})//只要有request就用这个
app.listen(3000, () =>{
console.log("listening on port 3000")
})
this is only served locally on my machine. localhost:3000
app.use((req, res) => {
console.log("we get the request")
res.send("hello, we got your request.this is a response")
})//只要有request就用这个
routing: taking incoming requests and a path that is requested and matching that to some code in some response.
app.get('/', (req, res) => {
res.send('this is the home page')
})
app.get('/r/:subreddit',(req, res) => {
res.send("this is a subreddit")
})//这个规定了符合这个模式的都可以匹配这个
app.get('/cats',(req,res) => {
console.log("cat request")
res.send('meow')
})
app.get('*',(req,res) => {
res.send('i dont know that path')
})//其他网址 注意不能放在最前面,否则之前的会被忽略了
app.get('/r/:subreddit',(req, res) => {
const {subreddit }= req.params;
res.send(`<h1>browsing the ${subreddit}`)
})
app.get('/r/:subreddit/:postId',(req, res) => {
const {subreddit,postId }= req.params;
res.send(`<h1>browsing Post ID: ${postId} the ${subreddit}`)
})
这样访问 localhost:3000/r/gardening/323
就会输出PostID323 the gardening
Query Strings
app.get('search', (req, res) => {
const {q } = req.query;
res.send(`<h1>search for ${q}</h1>`)
})
localhost:300/search?q=dog
auto-restart with Nodemon
改动代码使自动restart server
npm install -g nodemon
这样运行的时候用nodemon index.js
而不是node index.js