edit:guys I'm genually new to all of this. here's the html form I used. should i update this question with something else?
<form action="/pesquisar" method="post">
<input type="text" id="cO">
<input type="text" id="cD">
<input type="submit">
</form>
I'm currently trying to design a simple browser app utilizing express. console.log(req.body) comes back {} and i cant find the solution, been here for the best part of the day lol
Here's my app
var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.get('/', function(req, res){
console.log(req.body);
res.render('index', {
infoVoos: 'acesso_inicial'
});
});
app.post('/pesquisar', function(req,res){
console.log("");
console.log("");
console.log(req.body);
res.send('ok');
});
app.listen(3000);
console.log('############');
console.log('Server on');
console.log('');
module.exports = app;
I changed the parameter of the input tag from "id" to "name" and it's working fine now.
original
<form action="/pesquisar" method="post">
<input type="text" id="cO">
<input type="text" id="cD">
<input type="submit">
</form>
new
<form action="/pesquisar" method="post">
<input type="text" name="cO">
<input type="text" name="cD">
<input type="submit">
</form>
thankx guys
Related
so im trying to link the sign up page with the js page and i have no idea how, this is my forst time web developing so any advice is much much appreciated.
this is from a template i found online, what i want is to fill in the login info and have it say sommething like password and user dosent match?
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<div class='loginBox'>
<h2 style="color:#ec7079;">Login</h2>
<label for="username">Username: </label>
<input type="text" name="username" size="24">
<label for="password">Password: </label>
<input type="text" name="password" size="25">
<input type="checkbox" />
<label>Remember me </label><br />
<button id="buttonRegister">Log in</button><br />
Lost your password<br />
Back to Homepage
'use strict';
const path= require('path');
const express=require('express');
const app= express();
const user= require(path.join(__dirname,'./user.json'));
const server= require('http').Server(app);
const port= process.env.PORT || 3100;
const host= process.env.HOST ||'localhost';
app.use('/js', express.static(path.join(__dirname,'js')));
app.use('/style', express.static(path.join(__dirname,'style')));
app.use('/images', express.static(path.join(__dirname,'images')));
app.get('/', (req,res)=> res.sendFile(path.join(__dirname,'recipe.html')));
// app.use(express.static(path.join(__dirname,'html'))); //if wanna have HTML in one folder
app.get('/login',(req,res)=>res.sendFile(path.join(__dirname,'/login.html')));
app.get('/signup',(req,res)=>res.sendFile(path.join(__dirname,'/signup.html')));
app.get('/lostpassword',(req, res)=> res.sendFile(path.join(__dirname,'/lostpassword.html')));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.post('/login', express.urlencoded({extended:false}),(req,res)=>{
if(!req.body) return res.sendStatus(400);
if(user[req.body.username]==[req.body.password]){
res.render('login',{data:req.body, text:'Log in sucessfully', result:'You are now logged in'});
}else{
res.render('login',{data:req.body, text:'Incorrect password!', result:'Go back to login page and try again'});
}
});
app.post('/sending', function(req,res){
res.redirect('/');
});
server.listen(port,host,()=>
console.log(`Server ${host} on port ${port}`)
);
I have the following App.js:
var express = require('express'),
app = express(),
engines = require('consolidate'),
MongoClient = require('mongodb').MongoClient,
assert = require('assert'),
bodyParser = require('body-parser')
app.engine('html', engines.nunjucks);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended : true }));
// app.use(bodyParser.urlencoded());
// app.use(bodyParser.json());
app.post('/insert_movie', function (req, res) {
var movieName = req.body.movie_name;
console.log(movieName);
});
// No route matching:
app.use(function (req, res) {
res.sendStatus(404);
});
var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Express server listening on port %s.', port);
});
My html page:
<h1> Add new movies</h1>
<form action="/insert_movie" method="POST">
<input type="text" id="movie_name">
<input type="text" id="movie_year">
<input type="text" id="movie_imdb">
<input type="submit" value="Submit" />
</form>
When I enter values into the text boxes and press submit, my post method is hit ('/insert_movie'). However movieName is undefined not only that but req.body is {}
Can someone explain to me what I'm doing wrong here as I've gone through many solutions on this website however they're all pointing the body parser being incorrectly setup, I've tried the following:
app.use(bodyParser.urlencoded({ extended : true }));
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
Neither of these fix my issue.
You need to add name attribute to the input elements. That's one of the things your body-parser library needs to parse the form.
<h1> Add new movies</h1>
<form action="/insert_movie" method="POST">
<input type="text" name="movie-name" id="movie_name">
<input type="text" name="movie-year" id="movie_year">
<input type="text" name="movie-url" id="movie_imdb">
<input type="submit" value="Submit" />
</form>
try to use this
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
limit: '500mb',
extended: true,
parameterLimit: 50000
}));
app.use(expressValidator());
app.use(bodyParser.json());
use multer middle ware for req.body
var app = require('express')();
var multer = require('multer);
var upload = multer().any();
//multer().any() upload both array and file
//add the multer middle ware in your router
app.post('/insert_movie',upload, function (req, res) {
var movieName = req.body.movie_name;
console.log(req.body);
console.log(movieName);
});
you can see the official npm blog
https://www.npmjs.com/package/multer
Here is my html:
<form action='/new' method='POST' >
<span>pool name: </span>
<input type="text" name="name" />
<input type="hidden" name="imageSrcList" />
<button type='submit' >Create new</button>
</form>
And here is the relevant JS:
var app = express()
app.use(fileUpload());
app.set('view engine', 'ejs')
app.use(express.static(__dirname + '/views'));
app.post('/new', (req, res) => {
console.log(req.body.name);
})
The console reads out:
TypeError: Cannot read property 'name' of undefined
I have tried with console.log(req.body) and this also is undefined.
Thanks in advance!
You're missing the body-parser middleware which is necessary to have req.body set to a value. Express doesn't come with this by default and will need to be installed via NPM npm i --save body-parser
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
I am trying to get data from a form using express js.But i will get an undefined error in my console.
here is my html
<form action="/login" method="post">
<label>Username</label>
<input type="text" name="username" value=""><br><br>
<label>Password</label>
<input type="password" name="password" value=""><br><br>
<input type="submit" name="" value="Login">
</form>
index.js
var app = require('express')();
var url = require('url');
var server = require('http').Server(app);
var io = require('socket.io')(server);
var mongoose=require('mongoose');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// for login
app.post('/login',function(req,res){
console.log("server"+req.body.username+req.body.password);
});
I will get undefined error in my console,also i have installed body-parser in my application.
I'm not having issue with this code:
var app = require('express')();
var server = require('http').Server(app);
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/login',function(req,res){
console.log(req.body);
console.log("server"+req.body.username+req.body.password);
});
var server = app.listen(3000);
And testing with:
curl 'http://127.0.0.1:3000/login' --data 'username=zetg&password=zetgze'
I don't know what is wrong with your code, but try going slowly step by step, and including your functionalities one by one.
I'm using node.js for a basic form-processing task.
The html file loads, but when I submit the form, it fails to process.
Here is the code I'm running right now:
formhandling2.js
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', function(req, res){
res.sendfile('peanuts.html');
app.post('/myaction', function(req, res){
var userName = req.body.userName;
res.send ('Hello: ' + userName + '.<br>' + 'Try again.');
res.sendfile('peanuts.html');
});
app.listen(80);
peanuts.html
<html>
<head>
<title>Forms></title>
</head>
<body>
form action="/myaction" method="post">
<p>Enter your name:</p>
<input type="text" name="userName" placeholder="..." />
<br>
<button type="submit">Submit</button>
</body>
</html>
You have a syntax error in your server file
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', function(req, res){
res.sendfile('peanuts.html');
})
app.post('/myaction', function(req, res){
var userName = req.body.userName;
res.send ('Hello: ' + userName + '.<br>' + 'Try again.');
res.sendfile('peanuts.html');
});
app.listen(80);
See above, after res.sendfile('peanuts.html') you have to close }) braces.
then you have html syntax error you have not started form tag. Correct code below
<html>
<head>
<title>Forms></title>
</head>
<body>
<form action="/myaction" method="post">
<p>Enter your name:</p>
<input type="text" name="userName" placeholder="..." />
<br>
<button type="submit">Submit</button>
</body>
</html>
everything else works nice i tested once fixed