0

I am a newbie in node.js. I am using the following code. I have installed the express module and the related files have been installed.

var express = require('express')
var app = express.createServer()
app.listen(8000)

var tweets = []

app.get('/', function(req, res) {
  res.send('Welcome to Node Twitter')
})

app.post('/send', express.bodyParser(), function(req, res) {
  if (req.body && req.body.tweet) {
    tweets.push(req.body.tweet)
    res.send({status:"ok", message:"Tweet received"})
  } else {
    //no tweet?
    res.send({status:"nok", message:"No tweet received"})
  }
})

app.get('/tweets', function(req,res) {
  res.send(tweets)
})

I am getting the following error.

 var app = function(req, res, next) {
   app.handle(req, res, next);
 };

 mixin(app, proto);
 mixin(app, EventEmitter.prototype);

 app.request = { __proto__: req, app: app };
 app.response = { __proto__: res, app: app };
 app.init();
 return app;
 has no method 'createServer'
   at Object.<anonymous> (D:\ProgramFiles\Nodejs\node_modules\twitter.js:3:19)
   at Module._compile (module.js:456:26)
   at Object.Module._extensions..js (module.js:474:10)
   at Module.load (module.js:356:32)
   at Function.Module._load (module.js:312:12)
   at Function.Module.runMain (module.js:497:10)
   at startup (node.js:119:16)
   at node.js:906:3

This happens when I place the file outside the modules folder. When I place it somewhere else, it cant find the module also and returns the following error.

module.js:340
    throw err;
          ^
Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (D:\NodeApp\twitter.js:1:77)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

How to resolve this?

4
  • Have you installed with npm install express? Commented Jun 18, 2014 at 10:20
  • Yes. I installed using the line npm install express Commented Jun 18, 2014 at 10:27
  • AAAH. You are using a newer version of express which only might need var app = express(); Check your version of express :) Commented Jun 18, 2014 at 10:28
  • @goutam have you done this in your project? Commented Jun 18, 2014 at 12:03

1 Answer 1

3

Assuming a new version of express as @JoakimM suggested in comments, you would want to:

  1. update the var app = express() declaration to use the new syntax
  2. npm install body-parser --save to get the new body-parser middleware that no longer comes with Express 4.0 ( see: migrating from 3.x to 4.x docs)
  3. add a declaration to require the new middleware
  4. add app.use so that express knows about the new middleware
  5. remove the express.bodyparser argument from app.post('/send' route
  6. moved app.listen to the bottom. maybe this is superstition...but that's where I put it.
  7. I know this is grounds for a flamewar, but semicolons are your friend :) :) :)

Results of the above, worked on my system:

var express = require('express')
var bodyParser = require('body-parser')
var app = express()

var tweets = []

app.use(bodyParser())

app.get('/', function(req, res) {
  res.send('Welcome to Node Twitter')
})

app.post('/send', function(req, res) {
  if (req.body && req.body.tweet) {
    tweets.push(req.body.tweet)
    res.send({status:"ok", message:"Tweet received"})
  } else {
    //no tweet?
    res.send({status:"nok", message:"No tweet received"})
  }
})

app.get('/tweets', function(req,res) {
  res.send(tweets)
})

app.listen(8000)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.