How to use express-validator in nodeJs?
I am trying to use express-validator in my nodeJs example,
I trying a basic validation of whether username and password are valid,
username has to be an email address and
the password needs to be minimum 5 characters long.
I am using the example from the official doc
Following is the sample code I'm trying to run -
const express = require('express');
const app = express();
const check, validationResult = require('express-validator/check');
app.use(express.json());
app.all('/*', function (req, res, next)
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', "GET, POST, PUT, PATCH, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", 'Content-Type, Accept, X-Access-Token');
if (req.method == 'OPTIONS')
res.status(200).end();
else
next();
);
app.post('/user', [
// username must be an email
check('username').isEmail(),
// password must be at least 5 chars long
check('password').isLength( min: 5 )
], (req, res) =>
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty())
return res.status(422).json( errors: errors.array() );
User.create(
username: req.body.username,
password: req.body.password
).then(user => res.json(user));
);
var server = app.listen(8085, function ()
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
);
Following is the error I am getting when I call the API using POSTMAN -
For some reason, the validation fails despite passing a valid email as username and 5 characters long password,
What is the mistake, Can someone help?
node.js express express-validator
add a comment |
I am trying to use express-validator in my nodeJs example,
I trying a basic validation of whether username and password are valid,
username has to be an email address and
the password needs to be minimum 5 characters long.
I am using the example from the official doc
Following is the sample code I'm trying to run -
const express = require('express');
const app = express();
const check, validationResult = require('express-validator/check');
app.use(express.json());
app.all('/*', function (req, res, next)
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', "GET, POST, PUT, PATCH, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", 'Content-Type, Accept, X-Access-Token');
if (req.method == 'OPTIONS')
res.status(200).end();
else
next();
);
app.post('/user', [
// username must be an email
check('username').isEmail(),
// password must be at least 5 chars long
check('password').isLength( min: 5 )
], (req, res) =>
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty())
return res.status(422).json( errors: errors.array() );
User.create(
username: req.body.username,
password: req.body.password
).then(user => res.json(user));
);
var server = app.listen(8085, function ()
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
);
Following is the error I am getting when I call the API using POSTMAN -
For some reason, the validation fails despite passing a valid email as username and 5 characters long password,
What is the mistake, Can someone help?
node.js express express-validator
1
Look like an issue in yourBody
params in the request. UseJSON
instead ofText
.
– IftekharDani
Nov 15 '18 at 5:23
@IftekharDani doh! thanks, header Content-Type was missing.
– Aniruddha Raje
Nov 15 '18 at 5:39
add a comment |
I am trying to use express-validator in my nodeJs example,
I trying a basic validation of whether username and password are valid,
username has to be an email address and
the password needs to be minimum 5 characters long.
I am using the example from the official doc
Following is the sample code I'm trying to run -
const express = require('express');
const app = express();
const check, validationResult = require('express-validator/check');
app.use(express.json());
app.all('/*', function (req, res, next)
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', "GET, POST, PUT, PATCH, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", 'Content-Type, Accept, X-Access-Token');
if (req.method == 'OPTIONS')
res.status(200).end();
else
next();
);
app.post('/user', [
// username must be an email
check('username').isEmail(),
// password must be at least 5 chars long
check('password').isLength( min: 5 )
], (req, res) =>
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty())
return res.status(422).json( errors: errors.array() );
User.create(
username: req.body.username,
password: req.body.password
).then(user => res.json(user));
);
var server = app.listen(8085, function ()
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
);
Following is the error I am getting when I call the API using POSTMAN -
For some reason, the validation fails despite passing a valid email as username and 5 characters long password,
What is the mistake, Can someone help?
node.js express express-validator
I am trying to use express-validator in my nodeJs example,
I trying a basic validation of whether username and password are valid,
username has to be an email address and
the password needs to be minimum 5 characters long.
I am using the example from the official doc
Following is the sample code I'm trying to run -
const express = require('express');
const app = express();
const check, validationResult = require('express-validator/check');
app.use(express.json());
app.all('/*', function (req, res, next)
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', "GET, POST, PUT, PATCH, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", 'Content-Type, Accept, X-Access-Token');
if (req.method == 'OPTIONS')
res.status(200).end();
else
next();
);
app.post('/user', [
// username must be an email
check('username').isEmail(),
// password must be at least 5 chars long
check('password').isLength( min: 5 )
], (req, res) =>
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty())
return res.status(422).json( errors: errors.array() );
User.create(
username: req.body.username,
password: req.body.password
).then(user => res.json(user));
);
var server = app.listen(8085, function ()
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
);
Following is the error I am getting when I call the API using POSTMAN -
For some reason, the validation fails despite passing a valid email as username and 5 characters long password,
What is the mistake, Can someone help?
node.js express express-validator
node.js express express-validator
asked Nov 15 '18 at 4:22
Aniruddha RajeAniruddha Raje
5191233
5191233
1
Look like an issue in yourBody
params in the request. UseJSON
instead ofText
.
– IftekharDani
Nov 15 '18 at 5:23
@IftekharDani doh! thanks, header Content-Type was missing.
– Aniruddha Raje
Nov 15 '18 at 5:39
add a comment |
1
Look like an issue in yourBody
params in the request. UseJSON
instead ofText
.
– IftekharDani
Nov 15 '18 at 5:23
@IftekharDani doh! thanks, header Content-Type was missing.
– Aniruddha Raje
Nov 15 '18 at 5:39
1
1
Look like an issue in your
Body
params in the request. Use JSON
instead of Text
.– IftekharDani
Nov 15 '18 at 5:23
Look like an issue in your
Body
params in the request. Use JSON
instead of Text
.– IftekharDani
Nov 15 '18 at 5:23
@IftekharDani doh! thanks, header Content-Type was missing.
– Aniruddha Raje
Nov 15 '18 at 5:39
@IftekharDani doh! thanks, header Content-Type was missing.
– Aniruddha Raje
Nov 15 '18 at 5:39
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312402%2fhow-to-use-express-validator-in-nodejs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312402%2fhow-to-use-express-validator-in-nodejs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Look like an issue in your
Body
params in the request. UseJSON
instead ofText
.– IftekharDani
Nov 15 '18 at 5:23
@IftekharDani doh! thanks, header Content-Type was missing.
– Aniruddha Raje
Nov 15 '18 at 5:39