Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API routing_form_hello #9

Merged
merged 1 commit into from Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/pages/about.tsx
Expand Up @@ -3,7 +3,17 @@ const AboutPage = () => {
return (
<div className={styles.container}>
<h2>About page</h2>
<br />{" "}
<br />
<h3>API routing TEST</h3>
<form action="/api/form" method="post">
<label htmlFor="first">First name:</label>
<input type="text" id="first" name="first" />
<br />
<label htmlFor="last">Last name:</label>
<input type="text" id="last" name="last" />
<br />
<button type="submit">Submit</button>
</form>
</div>
);
};
Expand Down
19 changes: 19 additions & 0 deletions src/pages/api/form.tsx
@@ -0,0 +1,19 @@
export default function handler(req, res) {
// Get data submitted in request's body.
const body = req.body;

// Optional logging to see the responses
// in the command line where next.js app is running.
console.log("body: ", body);

// Guard clause checks for first and last name,
// and returns early if they are not found
if (!body.first || !body.last) {
// Sends a HTTP bad request error code
return res.status(400).json({ data: "First or last name not found" });
}

// Found the name.
// Sends a HTTP success code
res.status(200).json({ data: `${body.first} ${body.last}` });
}
4 changes: 4 additions & 0 deletions src/pages/api/hello.tsx
@@ -0,0 +1,4 @@
const handler = (req, res) => {
return res.status(200).json({ name: "へろー" });
};
export default handler;
6 changes: 3 additions & 3 deletions src/pages/index.tsx
Expand Up @@ -3,11 +3,11 @@ const RenderPage = () => (
<div className={styles.container}>
<div>
<h2>Home page</h2>
<p>branch 05_csb-e9r66s</p>
<p>branch 07_csb-rseo19</p>
<code>
making like easy-notion-blog
... making like easy-notion-blog
<br />
No component 
API routing 
</code>
</div>
</div>
Expand Down