How to use expressjs with connect?

匆匆过客 提交于 2019-12-20 04:57:07

问题


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'));
});

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?

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


回答1:


Quoting from the Express.js's middleware documentation,

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().

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());


来源:https://stackoverflow.com/questions/23442157/how-to-use-expressjs-with-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!