如何使用expressjs连接? [英] How to use expressjs with connect?

查看:118
本文介绍了如何使用expressjs连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var express = require('express'),
    routes = require('./routes'),
    http = require('http'),
    path = require('path'),
    fs = require('fs');

var app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(express.json());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', routes.index);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

我收到错误大多数中间件(如json)不再与...捆绑...

我已经在同一个文件夹中安装了 npm安装连接

但是如何使用它?

I'm getting the error Most middleware (like json) is no longer bundled with....
I've npm installed connect in the same folder.
But how do i use it?

我没有找到任何有关 connect&表达,他们或者谈论 connect express

I couldn't find any combined tutorial on connect & express, they either talk about connect or express.

推荐答案

Express中引用。 js的中间件文档


截至4.x,Express不再依赖于Connect。所有Express以前包含的中间件现在都分开回收。请查看中间件列表。唯一的中间件现在是 express.static()

As of 4.x, Express no longer depends on Connect. All of Express' previously included middleware are now in separate repos. Please view the list of middleware. The only included middleware is now express.static().

要使用 json 中间件,您需要添加 code> body-parser package.json 中,然后使用这样

So, to use json middleware, you need to include the body-parser in the package.json and then use like this

var bodyParser = require('body-parser');
app.use(bodyParser.json());

这篇关于如何使用expressjs连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆