I am new to nodeJS. I am trying to use different middlewares with connect middleware.
this is my code:
var connect = require('connect');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = connect()
.use(connect.bodyParser())
.use(connect.cookieParser('tobi is a cool ferret'))
.use(function(req, res){
console.log(req.cookies);
console.log(req.signedCookies);
res.end('hello\n');
}).listen(3000);
I have installed every middleware through npm.
I am getting this error while running this file.
/home/dipesh/Desktop/temp/temp.js:5
.use(connect.bodyParser())
^
TypeError: Object function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
} has no method 'bodyParser'
at Object.<anonymous> (/home/dipesh/Desktop/temp/temp.js:5:14)
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:929:3
Any suggestions?
.use(bodyParser())
not
.use(connect.bodyParser())
You have required body-parser, but then never used it.
You are essentially doing
var a = function(){};
var b = {};
b.a();
which is not correct because b has not 'a' property.
Related
Hello I'm having a silly problem but I can't find the solution. When I try start my localhost I get an error saying this :
Error: Cannot find module 'passport'
Require stack:
- D:\Other\Projects\Code\Powershell\shopping-cart\config\passport.js
- D:\Other\Projects\Code\Powershell\shopping-cart\routes\index.js
- D:\Other\Projects\Code\Powershell\shopping-cart\app.js
- D:\Other\Projects\Code\Powershell\shopping-cart\bin\www
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (D:\Other\Projects\Code\Powershell\shopping-cart\config\passport.js:1:16)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (D:\Other\Projects\Code\Powershell\shopping-cart\routes\index.js:5:16)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\config\\passport.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\routes\\index.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\app.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\bin\\www'
}
I understand that it is not finding the passport.js file, I have checked the path name more than 10 times and I saw another problem where the guy had not installed the passport module, so I did that as well but it is still throwing the same error. This is what my index.js looks like :
var express = require('express');
var router = express.Router();
var Product = require('D:/Other/Projects/Code/Powershell/shopping-cart/models/product');
var csrf = require('csurf');
var passport = require('D:/Other/Projects/Code/Powershell/shopping-cart/config/passport.js');
var csrfProtection = csrf();
router.use(csrfProtection);
/* GET home page. */
router.get('/', function(req, res, next) {
Product.find(function(err, docs) {
var productChunks = [];
var chunkSize = 3;
for (var i = 0; i < docs.length; i += chunkSize) {
productChunks.push(docs.slice(i, i + chunkSize));
}
res.render('shop/index', { title: 'Shopping Cart', products: productChunks });
});
});
router.get('/user/signup', function (req, res, next) {
res.render('user/signup', {csrfToken: req.csrfToken()});
});
router.post('/user/signup', passport.authenticate('local.signup', {
successRedirect: '/user/profile',
failureRedirect: '/user/signup',
failureFlash: true
}));
router.get('/user/profile', function(req, res, next) {
res.render('user/profile');
});
module.exports = router;
Any help would be appreciated, thanks! Also , I have another problem , can I post another question in like one hour? (is it allowed to have more than 1 questions posted?)
Im trying to create auto increment field in mongoose but i cant create for some reason
This is my app.js
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var Artist = require('./models/artist');
mongoose.connect('mongodb://localhost/auto-increment');
app.get('/', function(req, res){
Artist.create({
name: "artistName",
fpimage: "Cover"
}, function(err, artist){
if(err) {
console.log(err);
} else {
artist.save();
console.log(artist);
res.send('Hi ')
}
});
});
// Set Port
app.set('port', (process.env.PORT || 3000));
// Run Server
app.listen(app.get('port'), function(){
console.log('Server has started on Port: '+app.get('port'));
});
and this is my artist model
var mongoose = require("mongoose");
var autoIncrement = require('mongoose-auto-increment');
var artistSchema = new mongoose.Schema({
name: String,
fpimage: String
});
artistSchema.plugin(autoIncrement, 'Artist');
module.exports = mongoose.model('Artist', artistSchema);
When i run the app, i get following the error message
fn(this, opts);
^
TypeError: fn is not a function
at Schema.plugin (C:\Users\tjesu\Desktop\pidal\auto-increment\node_modules\mongoose\lib\schema.js:1060:3)
at Object.<anonymous> (C:\Users\tjesu\Desktop\pidal\auto-increment\models\artist.js:10:14)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\tjesu\Desktop\pidal\auto-increment\app.js:4:14)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
How can i fix this error? What should i do?
From the docs, it looks like you're missing two things:
You need to initialize autoIncrement:
autoIncrement.initialize(mongoose.connection);
You need to change your call to:
artistSchema.plugin(autoIncrement.plugin, 'Artist');
use autoIncrement.plugin instead of autoIncrement in your model
like:
artistSchema.plugin(autoIncrement.plugin, 'Artist');
and need initialize autoIncrement once
var connection = mongoose.createConnection("mongodb://localhost/auto-increment");
autoIncrement.initialize(connection);
and can use get like
app.get('/', function(req, res){
var artist = new Artist({name: "Telmuun",fpimage: "Cover"});
artist.save(function(err, saved) {
if(err) {
console.log(err);
}
else {
res.send('saved');
}
});
});
I want to add a script in app.js file here is the code:
app.helpers({
renderScriptsTags: function (all) {
if (all != undefined) {
return all.map(function(script) {
return '<script src="/javascript/' + script + '"></script>';
}).join('\n ');
}
else {
return '';
}
}
});
app.dynamicHelpers({
scripts: function(req, res) {
return ['canvasjs.min.js'];
}
});
while executing its give me an error shown below:
app.helpers({
^
TypeError: Object function (req, res, next) {
app.handle(req, res, next);
} has no method 'helpers'
at Object.<anonymous> (/var/www/html/nodeproject/helloworld/index.js:27:5)
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:902:3
As your error is showing app does not have helper funtion. Memeber functions of app are listed here http://expressjs.com/en/api.html
if you want to use helpers you will have to create them explicity like
//helper.js
module.export = function(){
// Your logic here
};
//usage
var helper = require('./relative/path/to/helper');
var getValueFromHelper = helper();
hope it helps :)
As a part of angular.js course, i downloaded and installed node.js, inserted server.js file in main folder with following content :
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
and then tried to run server by cli, but im getting error in cli:
TypeError: Object function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
} has no method 'static'
at Object.<anonymous> (C:\Program Files (x86)\nodejs\server.js:4: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
try:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('angularjs'));
app.listen(5000);
Should work for you!
EDIT:
With Connect 3.0 .static() is moved to a separate package called serve-static.
So you'll have to install that before running this code.
Edit: THis is for an older version of Node and therefore doesn't answer the question. See the comments below.
To use connect, you need to setup the environment, and then create the server
var connect = require('connect');
car app = connect();
app.static("../angularjs");
connect.createServer(app).listen(5000);
You may also be able to do it the brief way that you have by:
connect.createServer(
connect().static("../angularjs")
).listen(5000);
var express=require('express')
var app=express();
console.log("Encoded ",express.urlencoded());
app.use(express.urlencoded());
The above code throws the following error :
[user#localhost nodejs]$ node program.js
/home/user/Desktop/nodejs/program.js:41
console.log("Encoded ",express.urlencoded());
^
TypeError: Object function createApplication() {
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 'urlencoded'
at Object.<anonymous> (/home/user/Desktop/nodejs/program.js:41:32)
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:902:3
Seems like a similar question here - express.js trouble with connect modules but I'm already having express3.0.0 checked using the suggestion listed here - Find the version of an installed npm package
I also read the Api docs here - http://expressjs.com/api.html and they list urlencoded()
Please help.
I would also like to point, that I also tried using bodyParser() but that too gave the same error of has no method
The express guide is a bit-outdated.
For others having the same problem, the solution is that those methods have moved to a new module body-parser
Sample Code
var express=require('express');
var app=express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());