Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appengine/building-an-app/update/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"author": "Google Inc.",
"license": "Apache-2.0",
"dependencies": {
"cookie-parser": "^1.4.6",
"csurf": "^1.11.0",
"express": "^4.18.2"
},
"devDependencies": {
Expand Down
19 changes: 17 additions & 2 deletions appengine/building-an-app/update/server.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,37 @@

// [START gae_update_web_server_app]
const express = require('express');
const cookieParser = require('cookie-parser');
const csrf = require('csurf');
Comment on lines +19 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid hardcoding the HTML form in server.js and keep using the existing views/form.html template, we need to import the fs and path modules. This allows us to read the HTML file asynchronously and inject the CSRF token dynamically.

Suggested change
const cookieParser = require('cookie-parser');
const csrf = require('csurf');
const cookieParser = require('cookie-parser');
const csrf = require('csurf');
const fs = require('fs').promises;
const path = require('path');
References
  1. For asynchronous file system operations in Node.js, use the promise-based fs.promises API when working with async/await.

const fs = require('fs').promises;
const path = require('path');

const app = express();

// [START gae_enable_parser]
// This middleware is available in Express v4.16.0 onwards
app.use(cookieParser());
app.use(express.urlencoded({extended: true}));
app.use(csrf({cookie: true}));
// [END gae_enable_parser]

app.get('/', (req, res) => {
res.send('Hello from App Engine!');
});

// [START gae_add_display_form]
app.get('/submit', (req, res) => {
res.sendFile(path.join(__dirname, '/views/form.html'));
app.get('/submit', async (req, res, next) => {
try {
const token = req.csrfToken();
const template = await fs.readFile(path.join(__dirname, '/views/form.html'), 'utf-8');
const html = template.replace(
'<form method="POST" action="/submit">',
`<form method="POST" action="/submit"><input type="hidden" name="_csrf" value="${token}">`
);
res.send(html);
} catch (err) {
next(err);
}
});
// [END gae_add_display_form]

Expand Down