博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nodejs创建HTTPS服务器
阅读量:6156 次
发布时间:2019-06-21

本文共 5782 字,大约阅读时间需要 19 分钟。

hot3.png

http://blog.fens.me/nodejs-https-server/

express书第10章 有讲什么情况下用basic auth.下面是express中常用的中间件

Much of the middleware previously(before Express4.0) bundled with Express is quite fundamental, so it’s

important to know “where it went” and how to get it. You will almost always want
Connect, so it’s recommended that you always install it alongside Express (npm install
--save connect), and have it available in your application (var connect = re
quire(connect);).
basicAuth (app.use(connect.basicAuth)();)
Provides basic access authorization. Keep in mind that basic auth offers only the
most basic security, and you should use basic auth only over HTTPS (otherwise,
usernames and passwords are transmitted in the clear). You should use basic auth
only when you need something very quick and easy and you’re using HTTPS.

body-parser (npm install --save body-parser, app.use(require(bbody-

parser)());)
Convenience middleware that simply links in json and urlencoded. This
middleware is also still available in Connect, but will be removed in 3.0, so it’s
recommended that you start using this package instead. Unless you have a specific
reason to use json or urlencoded individually, I recommend using this package.
json (see body-parser)
Parses JSON-encoded request bodies. You’ll need this middleware if you’re writing
an API that’s expecting a JSON-encoded body. This is not currently very common
(most APIs still use application/x-www-form-urlencoded, which can be parsed
by the urlencoded middleware), but it does make your application robust and
future-proof.
urlencoded (see body-parser)
Parses request bodies with Internet media type application/x-www-form-
urlencoded. This is the most common way to handle forms and AJAX requests.

multipart (DEPRECATED)

Parses request bodies with Internet media type multipart/form-data. This mid‐
dleware is deprecated and will be removed in Connect 3.0. You should be using
Busboy or Formidable instead (see Chapter 8).
compress (app.use(connect.compress);)
Compresses response data with gzip. This is a good thing, and your users will thank
you, especially those on slow or mobile connections. It should be linked in early,
before any middleware that might send a response. The only thing that I recom‐
mend linking in before compress is debugging or logging middleware (which do
not send responses).
cookie-parser (npm install --save cookie-parser, app.use(require(cookie-
parser)(your secret goes here);
Provides cookie support. See Chapter 9.
cookie-session (npm install --save cookie-session,
app.use(require(cookie-session)());)
Provides cookie-storage session support. I do not generally recommend this ap‐
proach to sessions. Must be linked in after cookie-parser. See Chapter 9.
express-session (npm install --save express-session,
app.use(require(express-session)());)
Provides session ID (stored in a cookie) session support. Defaults to a memory
store, which is not suitable for production, and can be configured to use a database
store. See Chapters 9 and 13.
csurf (npm install --save csurf, app.use(require(csurf)());
Provides protection against cross-site request forgery (CSRF) attacks. Uses sessions,
so must be linked in after express-session middleware. Currently, this is identical
to the connect.csrf middleware. Unfortunately, simply linking this middleware
in does not magically protect against CSRF attacks; see Chapter 18 for more
information.
directory (app.use(connect.directory());)
Provides directory listing support for static files. There is no need to include this
middleware unless you specifically need directory listing.
errorhandler (npm install --save errorhandler, app.use(require(errorhan
dler)());
Provides stack traces and error messages to the client. I do not recommend linking
this in on a production server, as it exposes implementation details, which can have
security or privacy consequences. See Chapter 20 for more information.

static-favicon (npm install --save static-favicon,
app.use(require(static-favicon)(path_to_favicon));
Serves the “favicon” (the icon that appears in the title bar of your browser). This is
not strictly necessary: you can simply put a favicon.ico in the root of your static
directory, but this middleware can improve performance. If you use it, it should be
linked in very high in the middleware stack. It also allows you to designate a filename
other than favicon.ico.
morgan (previously logger, npm install --save morgan, app.use(require(mor
gan)());
Provides automated logging support: all requests will be logged. See Chapter 20 for
more information.
method-override (npm install --save method-override,
app.use(require(method-override)());
Provides support for the x-http-method-override request header, which allows
browsers to “fake” using HTTP methods other than GET and POST. This can be useful
for debugging. Only needed if you’re writing APIs.
query
Parses the querystring and makes it available as the query property on the request
object. This middleware is linked in implicitly by Express, so do not link it in
yourself.
response-time (npm install --save response-time,
app.use(require(response-time)());
Adds the X-Response-Time header to the response, providing the response time in
milliseconds. You usually don’t need this middleware unless you are doing perfor‐
mance tuning.
static (app.use(express.static(path_to_static_files)());
Provides support for serving static (public) files. You can link this middleware in
multiple times, specifying different directories. See Chapter 16 for more details.
vhost (npm install --save vhost, var vhost = require(vhost);
Virtual hosts (vhosts), a term borrowed from Apache, makes subdomains easier to
manage in Express. See Chapter 14 for more information.

转载于:https://my.oschina.net/uniquejava/blog/337206

你可能感兴趣的文章
10、程序员和编译器之间的关系
查看>>
配置 RAILS FOR JRUBY1.7.4
查看>>
AndroidStudio中导入SlidingMenu报错解决方案
查看>>
修改GRUB2背景图片
查看>>
Ajax异步
查看>>
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
查看>>
JAVA GC
查看>>
3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
查看>>
前端第七天
查看>>
图解SSH原理及两种登录方法
查看>>
【总结整理】JQuery基础学习---样式篇
查看>>
查询个人站点的文章、分类和标签查询
查看>>
基础知识:数字、字符串、列表 的类型及内置方法
查看>>
JSP的隐式对象
查看>>
JS图片跟着鼠标跑效果
查看>>
[SCOI2005][BZOJ 1084]最大子矩阵
查看>>
学习笔记之Data Visualization
查看>>
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
416. Partition Equal Subset Sum
查看>>
app内部H5测试点总结
查看>>