0

I am trying to export Connectas an middleware for an HTTP server test, but when I execute the code, it says the follow :

connect.createServer is not a function

My code :

hello_world.js

function helloWorld(req, res) {
res.end('Hello World!');
}
module.exports = helloWorld;

hello_world_app (where the problem is) :

var connect = require('connect');
// import middlewares
var helloWorld = require('./hello_world');
var app = connect.createServer(helloWorld);
app.listen(8080);

He points out to the hello_world_app in the var app, saying that is not a function. How can I make this work ?

1
  • 1
    I am sorry guys because I didn't looked at the documentation of Node.js, I am learning Node.js through a book, and it was like that in the example. Thanks for the answers and the patience. From now on, I am going to look to the documentation. Commented Jun 11, 2017 at 13:55

3 Answers 3

1

Looking at the docs for connect https://www.npmjs.com/package/connect - You are using it incorrectly

You would want something like this:

var connect = require('connect');
var helloWorld = require('./hello_world');
var http = require('http');

var app = connect();

app.use(helloWorld);

http.createServer(app).listen(8080);

Connect doesn't have a function called createServer which is why your code is erroring, that function exists in the http module.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok thanks for the answer, I am going to modify my code right now, I am new to Node.js so those errorrs are common for me.
1

You still need to create the http server with server with the http module. The 'connect' library just helps you use middleware more easily, so you have to plug the middleware into that.

var connect = require('connect');
// import middlewares
var helloWorld = require('./hello_world');

// Initiate the framework
var app = connect();

// Plug in your middleware
app.use(helloWorld);

// Tell http to use the framework
http.createServer(app).listen(8080);

1 Comment

Your example it's missing the require for http module. But all the rest is right, but anyway thanks for the answer.
1

The connect documentation states that you use it this way:

var connect = require('connect');
var http = require('http');
var app = connect();
http.createServer(app).listen(3000);

https://www.npmjs.com/package/connect

1 Comment

Thank you for your answer, it really helps me too that you put the link from the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.