node使用自定义模块跟express

这篇博客探讨了如何在Node.js环境中创建和使用自定义模块,详细讲解了npm发布的流程。接着,文章深入介绍了Express框架,包括响应请求、GET与POST请求的处理、express-static中间件的使用,以及如何进行表达式的综合练习。此外,还讨论了如何获取GET和POST参数,并展示了编写自定义中间件的方法。最后,作者提供了加入专业学习群获取更多课程资源的邀请。
摘要由CSDN通过智能技术生成

自定义模块

require——引入其他模块
exports——输出
module——批量输出



自定义模块向外输出


/*
exports.a=12;
exports.b=5;
exports.c=99;
*/


//var a=12;
exports.a=12;

获取自定义模块输出内容

const mod=require('mod');

console.log(mod.a);



批量向外输出
module.exports={a: 12, b: 5, c: 99};

npm

npm:NodeJS Package Manager(NodeJS包管理器)
1.统一下载途径
2.自动下载依赖


node_modules——放模块
./
不加./		必须放在node_modules里面
require
1.如果有"./"
	从当前目录找

2.如果没有"./"
	先从系统模块
	再从node_modules找
	
	自定义模块统一,都放到node_modules里面

npm发布自己的模块

https://www.npmjs.com/

登陆自己的npm账号
npm login

Username: zhf198821
Password:
Email: (this IS public) 349860782@qq.com
Logged in as zhf198821 on https://registry.npmjs.org/.


npm whoami 查看自己的信息


npm init 初始化
Press ^C at any time to quit.
package name: (自定义模块) mytest
version: (1.0.0)
description: 这是我的测试包
entry point: (index.js)
test command:
git repository:
keywords:

生成信息
{
  "name": "mytest",
  "version": "1.0.0",
  "description": "这是我的测试包",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

npm publish

如果无法发布说明该包名字被其他人占有

跟新后发布据需要重新发布
npm update 包名




express模块

安装
  cnpm install express


express模块响应请求

const express=require('express');

var server=express();



server.use('/', function (req.res){
  console.log('use了');
  
});

server.listen(8080);


express中res req

非侵入式  增强
req

原生:
res.write();
res.end();

express:
*res.send();
res.write();
res.end();

send可以发送 json对象

res.send({a:11,b:12})

express中处理get与post

//server.get('/', function (){
//console.log('有GET');
//});
//server.post('/', function (){
//console.log('有POST');
//});

express中使用express-static中间件

server.use(expressStatic('./www'));

express综合练习

const express=require('express');
const expressStatic=require('express-static');

var server=express();
server.listen(8080);

//用户数据
var users={
   
  'blue': '123456',
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值