Reputation: 75
Been learning Node.js so far, building some MVC application but still cannot handle form properly, and I need some help to figure out how things work.
Okay, the best way to figure how this thing works is on my previous question, Steam Web API programming.
I installed express and steamwebapi. My index.html is looking like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Steam</title>
</head>
<body>
<form action="/ask_user_id" method="POST">
<input type="text" name="id"><br>
<input type="submit" value="Pošalji">
</form>
</body>
</html>
My app.js:
//Express
var express = require('express');
var server = express();
//Steam
var SteamWebAPI = require('steamwebapi').SteamWebAPI;
SteamWebAPI.setAPIKey('xxx key is here ');
//Rendering form in index.html file
server.get('/ask_user_id', function(req, res) {
app.render('form', function(err, html) {
if(err)
return res.send(err);
else
return res.send(html)
});
});
//Route for handling form submission
server.post('/user_infos', function(req, res) {
//Form submitted in req.body, retriving userID
var _userID = req.body.userId;
//SteamWebAPI - getRecentlyPlayedGames
SteamWebAPI.getRecentlyPlayedGames(_userID, 5, function(response) {
return res.json(response.response.games);
});
});
//Localhost
server.listen(3000, function() {
console.log('Server: 3000');
});
I'm using IntelliJ IDEA. I type in terminal Node App.js, server:3000. If I go to my index.html file, it redirected me to http://localhost:63342/Steam/index.html, and if I type something in form, it redirect me to: http://localhost:63342/ask_user_id and I got "404 Not Found".
What I'm doing wrong? If I type node app.js and then go to localhost:3000/ask_user_id I got reference error. Someone once asked me why I need app there; I don't know, if I put server it results in an error again.
Upvotes: 0
Views: 2548
Reputation: 3332
in your html, change to action="/user_infos"
the request will go like this
GET /ask_user_id <- this is you going to localhost:3000/ask_user_id in browser
POST /user_infos <- this is you submitting the data to the URL specified in the action attribute of your form.
Upvotes: 1
Reputation: 2156
the post actio in client is "ask_user_id"
change the server to listen
server.post('/ask_user_id',
no server.get
Upvotes: 0