Create form get value submit (ok)
C:\Users\Administrator\Desktop\backend\static\login.html
<!DOCTYPE html>
<html>
<head>
<title>Example Login Form</title>
</head>
<body>
<form action="http://localhost:5000/webhook" method="post">
<!-- user input-->
Username:<br>
<input type="text" name="username" placeholder="Username" required><br><br>
Password:<br>
<input type="password" name="password" placeholder="Password" required><br><br>
<!-- submit button -->
<input type="submit" value="login">
</form>
</body>
</html>
C:\Users\Administrator\Desktop\backend\static\index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome Home</title>
</head>
<body>
<!-- link to login page-->
<a href="/webhook">Please Login Here!</a>
</body>
</html>
C:\Users\Administrator\Desktop\backend\app.js
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 5000
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
const corsOptions = {
origin: "http://localhost:3000"
};
// const requestEndpoint = "https://c3o3jb4pevh6.cybozu.com/k/v1/records.json?app=18&id=131700";
const requestEndpoint = "https://c3o3jb4pevh6.cybozu.com/k/v1/records.json?app=18";
const requestEndpoint2 = "https://c3o3jb4pevh6.cybozu.com/k/v1/records.json?app=43";
// This function runs if the http://localhost:5000/getData endpoint
// is requested with a GET request
const fetchOptions = {
method: 'GET',
headers: {
'X-Cybozu-API-Token': process.env.API_TOKEN
}
}
const fetchOptions2 = {
method: 'GET',
headers: {
'X-Cybozu-API-Token': process.env.API_TOKEN2
}
}
// Route to Homepage
app.get('/', (req, res) => {
res.sendFile(__dirname + '/static/index.html');
});
// Route to Login Page
app.get('/webhook', (req, res) => {
res.sendFile(__dirname + '/static/login.html');
});
app.post('/webhook',async (req, res) => {
// Insert Login Code Here
let username = req.body.username;
let password = req.body.password;
console.log(username);
console.log(password);
// const response = await fetch(requestEndpoint, fetchOptions);
// const jsonResponse = await response.json();
// const response2 = await fetch(requestEndpoint2, fetchOptions2);
// const jsonResponse2 = await response2.json();
});
// app.get('/getData', cors(corsOptions), async (req, res) => {
// const response = await fetch(requestEndpoint, fetchOptions);
// const jsonResponse = await response.json();
// const response2 = await fetch(requestEndpoint2, fetchOptions2);
// const jsonResponse2 = await response2.json();
// console.log(jsonResponse.records);
// console.log("aaaaaaa1");
// console.log(jsonResponse2);
// res.json(jsonResponse);
// });
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`);
});
C:\Users\Administrator\Desktop\backend\package.json
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"node-fetch": "^2.6.7"
}
}
PreviousLiên kết group chat line (ok)NextHow To Use EJS to Template Your Node Application, data (ok)
Last updated