I have an issue with a Contact Form. I'm just trying to make a simple post with express.
I have the following code:
views/contacto.jade
extends layout-inside
block content
.main-container
form(action='contacto', name='contacto', id='contacto', method='post')
section.cover.fullscreen.image-bg.overlay
.background-image-holder
img.background-image(alt='image', src='/images/bg-6.jpg')
|
.container.v-align-transform
.row
.col-sm-6.col-sm-offset-3
.feature.bordered.text-center
h4.uppercase Contacto
|
form.text-left
input(type='text', name='name', placeholder='Nombre')
|
input(type='text', name='email', placeholder='Email')
|
input(type='text', name="telefono", placeholder='Teléfono')
|
textarea(name="mensaje", placeholder='mensaje')
|
input(type='submit', value='Enviar')
|
routes/index.js
router.get('/contacto', function(req, res, next) {
res.render('contacto', { title: 'Contacto'});
});
And when I do the post in the form I got this error:
Error: Not Found
at /Users/dev/node/website/app.js:33:13
at Layer.handle [as handle_request] (/Users/dev/node/website/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/dev/node/website/node_modules/express/lib/router/index.js:312:13)
at /Users/dev/node/website/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/dev/node/website/node_modules/express/lib/router/index.js:330:12)
at next (/Users/dev/node/website/node_modules/express/lib/router/index.js:271:10)
at /Users/dev/node/website/node_modules/express/lib/router/index.js:618:15
at next (/Users/dev/node/website/node_modules/express/lib/router/index.js:256:14)
at Function.handle (/Users/dev/node/website/node_modules/express/lib/router/index.js:176:3)
at router (/Users/dev/node/website/node_modules/express/lib/router/index.js:46:12)
Do you have any idea why this is happening? It appears that the file is not there.
Thanks
They're distinct HTTP verbs, GET
isn't same as POST
, when doing a GET /contactos
you render back a view, that's ok. When clicking on submit you will attempt to perform a POST request
.
You may add something like this to handle the form submit via POST:
router.post('/contacto', function(req, res, next) {
var formData = request.body;
// Do stuff
return response.end(201);
});
For your API, you should prefix routes with /api, for example: /api/contacts
. Also, you should code it all in english.
You should use a Router
object to setup your routes as explained in this other post .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.