Hi,
I cannot to find the bug. Is anyone can help me?
When I submit the form I get this message in the browser:
cannot POST /articles/crate
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<link rel="stylesheet" href="style/style.css" />
<title>Build an Image Search Abstraction Layer</title>
</head>
<body>
<div class="navbar">
<a>Overloop</a>
<a class="right">Articles</a>
</div>
<div class="articles">
<button>Create a new Article</button>
</div>
<div class="newArticleForm hide">
<form action="/articles/create" method="post">
<div class="row">
<div class="col25">
<label for="country">Country</label>
</div>
<div class="col75">
<select id="country" name="country" multiple="multiple">
<!-- options created dinamically with javascript -->
<option value="france">France</option>
<option value="belgium">Belgium</option>
<option value="uk">United Kingdom</option>
</select>
</div>
</div>
<div class="row">
<div class="col25">
<label for="title">Title</label>
</div>
<div class="col75">
<input type="text" id="title" name="title" placeholder="Title" />
</div>
</div>
<div class="row">
<div class="col25">
<label for="content">Content</label>
</div>
<div class="col75">
<textarea id="content" name="content" placeholder="Article Content"></textarea>
</div>
</div>
<div class="row">
<div class="col25"></div>
<div class="col75">
<input type="submit" value="Submit" />
<input type="button" value="Cancel" />
</div>
</div>
</form>
</div>
<script src="main.js"></script>
</body>
</html>
server.js
"use strict";
const express = require("express");
const app = express();
const bodyParser = require('body-parser');
const port = 3030;
// serve a static client:
app.use(express.static("public"));
// link the app... .js
app.use(require("./app/get.js"));
app.use(require("./app/post.js"));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
post.js
"use strict";
const express = require('express');
const app = module.exports = express();
// needs to parse POST bodies:
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: false
}));
const checkPostData = (request, response, next) => {
const warning = "bad";
console.log(request);
console.log(response);
if (warning) return response.send(warning);
next();
};
const addNewArticle = (request, response) => {
console.log("hello article");
console.log(request);
console.log(response);
response.send("bad");
};
// POST form Create a new Article:
app.post("/articles/create", checkPostData, addNewArticle);
I also uploaded to the Glitch.
On console I see the server is running but when I start the app on Glitch it is never wake up. On localhost the index.html rendered well.
What the … I forget?
updated: On Glitch I found why not wake up the project. I changed the port to 3000. After that the html rendered well but I have same issue like localhost: cannot POST…
Oh my got… when I changed the port to 3000, it’s already works on localhost too… But why not work on another port???