JackSlayer94
JackSlayer94

Reputation: 855

Hello World Node.js/Express application error

I am learning MEAN stack so started with Node.js and Express first. I run Nginx on Windows 10. I installed the latest Node. NPM version is 3.10.3, Node version is 6.7.0, and Express version is 4.14.0. I did npm install express --save, npm install mongoose --save, npm install body-parser --save, and created the server.js file. I did node server on my sources and I got the following error:

C:\nginx\html>node server
C:\nginx\html\server.js:7
app.get('/', function(req, res){
    ^

TypeError: app.get is not a function
    at Object. (C:\nginx\html\server.js:7:5)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

C:\nginx\html>

And the code in my server.js file is:

var express = require('express');
var express = require('mongoose');
var express = require('body-parser');

var app = express();

app.get('/', function(req, res){
  res.send('hello world');
});

app.listen(80);

I am not sure how a hello world application can go wrong. I saw a lot of tuts and code with the same thing but working for them. Even the Express's official hello world app has same code.

Upvotes: 0

Views: 393

Answers (2)

Arif Khan
Arif Khan

Reputation: 5069

You need to install express not expression as following

  npm install express --save

and then you like play

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

var app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())

app.get('/', function(req, res){
  res.send('hello world');
});

app.post('/', function(req, res){
  res.json({data: req.body});
});

app.listen(3000);

and then try to listen another port that is above 1024 may be 3000 or 8080 or anything else otherwise you have to run cmd as Administrator

Upvotes: 0

Yash Goenka
Yash Goenka

Reputation: 110

try this

var express = require('express');
var mongoose= require('mongoose');
var bodyparser = require('body-parser');

var app = express();

app.get('/', function(req, res){
 res.send('hello world');
});

app.listen(80);

Upvotes: 2

Related Questions