Nodejs学习记录:express

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

npm express

简介

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

实例

先来个helloworld简单实例

先新建01-express文件夹 安装express并将其保存到依赖列表中

  cnpm install express --save
  

再安装几个重要模块

 cnpm install body-parser --save
 cnpm install cookie-parser --save
 cnpm install multer --save

body-parser
Nodejs进阶:Express常用中间件body-parser实现解析

图片描述
看下 express 使用的版本号

 cnpm list express

图片描述

代码

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World');
});

app.listen(3000, function () {
  console.log('app is listening at port 3000');
});

图片描述

express vs 原始代码

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('this is the homepage');
});

app.get('/contact', function (req, res) {
  res.send('this is the contact page');
});

app.listen(3000, function () {
  console.log('app is listening at port 3000');
});
//get:  app.get('route',fn)
//post:  app.post('route',fn)
//delete:  app.delete('route',fn)
var http = require('http');
var fs = require('fs');

var server = http.createServer(function(req, res) {
    console.log('request was made: ' + req.url);

    if (req.url === '/home' || req.url === '/'){

        res.writeHead(200,{'Content-Type': 'text/html'});
      fs.createReadStream(__dirname + '/index.html','utf8').pipe(res);
      
    }else  if(req.url === '/contact'){
  res.writeHead(200,{'Content-Type': 'text/html'});
  fs.createReadStream(__dirname + '/contact.html','utf8').pipe(res);
        
    }else  if(req.url === '/api/users'){
        var users = [{name: 'AlexZ33', age:26}, {name: 'jingxin', age: 8}];

        res.writeHead(200, {'Content-Type': 'application/json'});
        res.end(JSON.stringify(users));

    }else {
    res.writeHead(404,{'Content-Type': 'text/html'});
  fs.createReadStream(__dirname + '/404.html','utf8').pipe(res);
    }
    
});

server.listen(3000,'127.0.0.1');
console.log('now listening to port 3000');

上面一种是不是简单太多。

完整的项目开发过程

http://y.dobit.top/Detail/150...

常用中间件和开发包:

multer: Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files
morgan: HTTP request logger middleware for node.js
body-parser: Node.js body parsing middleware.

Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
pm2: 进程管理的,一般用于生产环境
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
rotating-file-stream: Creates a stream.Writable to a file which is rotated. Rotation behaviour can be deeply customized; optionally, classical UNIX logrotate behaviour can be used.
which: Like the unix which utility.

Finds the first instance of a specified executable in the PATH environment variable. Does not cache the results, so hash -r is not needed when the PATH changes.
chalk: Terminal string styling done right

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看