Code files
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const express = require('express')
|
||||
const bodyParser = require('body-parser')
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.use(bodyParser.urlencoded({extended: false}))
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.17.1",
|
||||
"express": "^4.15.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/:name?', function (req, res) {
|
||||
const title = 'Express'
|
||||
// CAUTION: never place user input
|
||||
// directly into HTML output in production
|
||||
// without sanitizing it first. Otherwise, we make
|
||||
// ourselves vulnerable to XSS attacks.
|
||||
// See Chapter 8 Dealing with Security for details
|
||||
const name = req.params.name
|
||||
res.send(`
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title}${name ? `, ${name}.` : ''} </p>
|
||||
<form method=POST action=data>
|
||||
Name: <input name=name> <input type=submit>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
})
|
||||
|
||||
router.post('/data', function (req, res) {
|
||||
res.redirect(`/${req.body.name}`)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
Reference in New Issue
Block a user