{"exhaustive":{"nbHits":false,"typo":false},"exhaustiveNbHits":false,"exhaustiveTypo":false,"hits":[{"_highlightResult":{"author":{"matchLevel":"none","matchedWords":[],"value":"fghsss"},"comment_text":{"fullyHighlighted":false,"matchLevel":"full","matchedWords":["sql","\u0437\u0430\u043f\u0440\u043e\u0441"],"value":"\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u044f \u043c\u043e\u0433\u0443 \u043f\u043e\u043c\u043e\u0447\u044c \u0441 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u043a\u043e\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u044b \u0443\u043a\u0430\u0437\u0430\u043b. \u0414\u0430\u0432\u0430\u0439 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0435\u0440\u0441\u0438\u044e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c `MySQL` \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c `Node.js` \u0438 `Express.js` \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f API \u0438 `MySQL` \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<p>### \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<p>\u041f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b:\n- `server.js`: \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u0441\u0435\u0440\u0432\u0435\u0440\u0430.\n- `db.js`: \u0424\u0430\u0439\u043b \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.\n- `models/surveyModel.js`: \u041c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u043e\u043f\u0440\u043e\u0441\u043e\u0432.\n- `routes/surveyRoutes.js`: \u0420\u043e\u0443\u0442\u044b \u0434\u043b\u044f CRUD-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u043e\u043f\u0440\u043e\u0441\u0430\u043c\u0438.<p>### \u0428\u0430\u0433\u0438 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<p>#### 1. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439\n\u0412 \u043f\u0430\u043f\u043a\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438:<p>```bash\nnpm init -y\nnpm install express mysql2\n```<p>#### 2. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 (`db.js`)<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b `db.js` \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a MySQL.<p>```javascript\n// db.js\nconst mysql = require('mysql2');\nconst connection = mysql.createConnection({\n  host: 'localhost',\n  user: 'root',\n  password: 'yourpassword',\n  database: 'survey_db'\n});<p>connection.connect((err) =&gt; {\n  if (err) throw err;\n  console.log('Connected to MySQL database');\n});<p>module.exports = connection;\n```<p>#### 3. \u041c\u043e\u0434\u0435\u043b\u044c \u043e\u043f\u0440\u043e\u0441\u0430 (`models/surveyModel.js`)<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u043e\u043f\u0440\u043e\u0441\u043e\u0432.<p>```javascript\n// models/surveyModel.js\nconst db = require('../db');<p>const Survey = {\n  create: (title, description, callback) =&gt; {\n    const query = 'INSERT INTO surveys (title, description, editable) VALUES (?, ?, ?)';\n    db.query(query, [title, description, true], callback);\n  },<p><pre><code>  update: (id, title, description, callback) =&gt; {\n    const query = 'UPDATE surveys SET title = ?, description = ?, editable = ? WHERE id = ? AND editable = true';\n    db.query(query, [title, description, false, id], callback);\n  },\n\n  delete: (id, callback) =&gt; {\n    const query = 'DELETE FROM surveys WHERE id = ?';\n    db.query(query, [id], callback);\n  },\n\n  getAll: (callback) =&gt; {\n    const query = 'SELECT * FROM surveys';\n    db.query(query, callback);\n  }</code></pre>\n};<p>module.exports = Survey;\n```<p>#### 4. \u0420\u043e\u0443\u0442\u044b \u0434\u043b\u044f \u043e\u043f\u0440\u043e\u0441\u043e\u0432 (`routes/surveyRoutes.js`)<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0440\u043e\u0443\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u043f\u0440\u043e\u0441\u0430\u043c\u0438.<p>```javascript\n// routes/surveyRoutes.js\nconst express = require('express');\nconst router = express.Router();\nconst Survey = require('../models/surveyModel');<p>// \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043e\u043f\u0440\u043e\u0441\nrouter.post('/create', (req, res) =&gt; {\n  const { title, description } = req.body;\n  Survey.create(title, description, (err, results) =&gt; {\n    if (err) return res.status(500).json(err);\n    res.status(201).json({ message: 'Survey created', surveyId: results.insertId });\n  });\n});<p>// \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u043f\u0440\u043e\u0441 (\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437)\nrouter.put('/update/:id', (req, res) =&gt; {\n  const { id } = req.params;\n  const { title, description } = req.body;\n  Survey.update(id, title, description, (err, results) =&gt; {\n    if (err) return res.status(500).json(err);\n    if (results.affectedRows === 0) return res.status(403).json({ message: 'Survey cannot be edited' });\n    res.json({ message: 'Survey updated' });\n  });\n});<p>// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u043f\u0440\u043e\u0441\nrouter.delete('/delete/:id', (req, res) =&gt; {\n  const { id } = req.params;\n  Survey.delete(id, (err, results) =&gt; {\n    if (err) return res.status(500).json(err);\n    res.json({ message: 'Survey deleted' });\n  });\n});<p>// \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u043f\u0440\u043e\u0441\u044b\nrouter.get('/', (req, res) =&gt; {\n  Survey.getAll((err, results) =&gt; {\n    if (err) return res.status(500).json(err);\n    res.json(results);\n  });\n});<p>module.exports = router;\n```<p>#### 5. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 \u0444\u0430\u0439\u043b (`server.js`)<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0440\u043e\u0443\u0442\u043e\u0432.<p>```javascript\n// server.js\nconst express = require('express');\nconst app = express();\nconst surveyRoutes = require('./routes/surveyRoutes');<p>app.use(express.json());\napp.use('/api/surveys', surveyRoutes);<p>const PORT = 3000;\napp.listen(PORT, () =&gt; {\n  console.log(`Server is running on port ${PORT}`);\n});\n```<p>#### 6. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b `surveys` \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 <em>SQL</em>-<em>\u0437\u0430\u043f\u0440\u043e\u0441</em> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<p>```<em>sql</em>\nCREATE TABLE surveys (\n  id INT AUTO_INCREMENT PRIMARY KEY,\n  title VARCHAR(255) NOT NULL,\n  description TEXT,\n  editable BOOLEAN DEFAULT TRUE\n);\n```<p>### \u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0430<p>1. \u0423\u0431\u0435\u0434\u0438\u0441\u044c, \u0447\u0442\u043e MySQL \u0441\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u0438 \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u0430.\n2. \u0417\u0430\u043f\u0443\u0441\u0442\u0438 \u0441\u0435\u0440\u0432\u0435\u0440:<p>```bash\nnode server.js\n```<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0442\u044b \u043c\u043e\u0436\u0435\u0448\u044c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c API, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u043e\u0434\u0435 Postman \u0438\u043b\u0438 curl \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443:<p>- `POST /api/surveys/create` \u2014 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043e\u043f\u0440\u043e\u0441.\n- `PUT /api/surveys/update/:id` \u2014 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u043f\u0440\u043e\u0441 (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437).\n- `DELETE /api/surveys/delete/:id` \u2014 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u043f\u0440\u043e\u0441.\n- `GET /api/surveys` \u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u043f\u0440\u043e\u0441\u044b.<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u0442\u0432\u043e\u0438 \u0437\u0430\u0434\u0430\u0447\u0438. \u0423\u0434\u0430\u0447\u0438 \u043d\u0430 \u0441\u043e\u0440\u0435\u0432\u043d\u043e\u0432\u0430\u043d\u0438\u044f\u0445!"},"story_title":{"matchLevel":"none","matchedWords":[],"value":"Show HN: Wewod \u2013 A Social Score Tracker for CrossFit WODs"},"story_url":{"matchLevel":"none","matchedWords":[],"value":"https://wewod.app/"}},"_tags":["comment","author_fghsss","story_42127881"],"author":"fghsss","comment_text":"\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u044f \u043c\u043e\u0433\u0443 \u043f\u043e\u043c\u043e\u0447\u044c \u0441 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u043a\u043e\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u044b \u0443\u043a\u0430\u0437\u0430\u043b. \u0414\u0430\u0432\u0430\u0439 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0435\u0440\u0441\u0438\u044e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c `MySQL` \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c `Node.js` \u0438 `Express.js` \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f API \u0438 `MySQL` \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<p>### \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<p>\u041f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b:\n- `server.js`: \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u0441\u0435\u0440\u0432\u0435\u0440\u0430.\n- `db.js`: \u0424\u0430\u0439\u043b \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.\n- `models&#x2F;surveyModel.js`: \u041c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u043e\u043f\u0440\u043e\u0441\u043e\u0432.\n- `routes&#x2F;surveyRoutes.js`: \u0420\u043e\u0443\u0442\u044b \u0434\u043b\u044f CRUD-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u043e\u043f\u0440\u043e\u0441\u0430\u043c\u0438.<p>### \u0428\u0430\u0433\u0438 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<p>#### 1. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439\n\u0412 \u043f\u0430\u043f\u043a\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438:<p>```bash\nnpm init -y\nnpm install express mysql2\n```<p>#### 2. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 (`db.js`)<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b `db.js` \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a MySQL.<p>```javascript\n&#x2F;&#x2F; db.js\nconst mysql = require(&#x27;mysql2&#x27;);\nconst connection = mysql.createConnection({\n  host: &#x27;localhost&#x27;,\n  user: &#x27;root&#x27;,\n  password: &#x27;yourpassword&#x27;,\n  database: &#x27;survey_db&#x27;\n});<p>connection.connect((err) =&gt; {\n  if (err) throw err;\n  console.log(&#x27;Connected to MySQL database&#x27;);\n});<p>module.exports = connection;\n```<p>#### 3. \u041c\u043e\u0434\u0435\u043b\u044c \u043e\u043f\u0440\u043e\u0441\u0430 (`models&#x2F;surveyModel.js`)<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u043e\u043f\u0440\u043e\u0441\u043e\u0432.<p>```javascript\n&#x2F;&#x2F; models&#x2F;surveyModel.js\nconst db = require(&#x27;..&#x2F;db&#x27;);<p>const Survey = {\n  create: (title, description, callback) =&gt; {\n    const query = &#x27;INSERT INTO surveys (title, description, editable) VALUES (?, ?, ?)&#x27;;\n    db.query(query, [title, description, true], callback);\n  },<p><pre><code>  update: (id, title, description, callback) =&gt; {\n    const query = &#x27;UPDATE surveys SET title = ?, description = ?, editable = ? WHERE id = ? AND editable = true&#x27;;\n    db.query(query, [title, description, false, id], callback);\n  },\n\n  delete: (id, callback) =&gt; {\n    const query = &#x27;DELETE FROM surveys WHERE id = ?&#x27;;\n    db.query(query, [id], callback);\n  },\n\n  getAll: (callback) =&gt; {\n    const query = &#x27;SELECT * FROM surveys&#x27;;\n    db.query(query, callback);\n  }</code></pre>\n};<p>module.exports = Survey;\n```<p>#### 4. \u0420\u043e\u0443\u0442\u044b \u0434\u043b\u044f \u043e\u043f\u0440\u043e\u0441\u043e\u0432 (`routes&#x2F;surveyRoutes.js`)<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0440\u043e\u0443\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u043f\u0440\u043e\u0441\u0430\u043c\u0438.<p>```javascript\n&#x2F;&#x2F; routes&#x2F;surveyRoutes.js\nconst express = require(&#x27;express&#x27;);\nconst router = express.Router();\nconst Survey = require(&#x27;..&#x2F;models&#x2F;surveyModel&#x27;);<p>&#x2F;&#x2F; \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043e\u043f\u0440\u043e\u0441\nrouter.post(&#x27;&#x2F;create&#x27;, (req, res) =&gt; {\n  const { title, description } = req.body;\n  Survey.create(title, description, (err, results) =&gt; {\n    if (err) return res.status(500).json(err);\n    res.status(201).json({ message: &#x27;Survey created&#x27;, surveyId: results.insertId });\n  });\n});<p>&#x2F;&#x2F; \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u043f\u0440\u043e\u0441 (\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437)\nrouter.put(&#x27;&#x2F;update&#x2F;:id&#x27;, (req, res) =&gt; {\n  const { id } = req.params;\n  const { title, description } = req.body;\n  Survey.update(id, title, description, (err, results) =&gt; {\n    if (err) return res.status(500).json(err);\n    if (results.affectedRows === 0) return res.status(403).json({ message: &#x27;Survey cannot be edited&#x27; });\n    res.json({ message: &#x27;Survey updated&#x27; });\n  });\n});<p>&#x2F;&#x2F; \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u043f\u0440\u043e\u0441\nrouter.delete(&#x27;&#x2F;delete&#x2F;:id&#x27;, (req, res) =&gt; {\n  const { id } = req.params;\n  Survey.delete(id, (err, results) =&gt; {\n    if (err) return res.status(500).json(err);\n    res.json({ message: &#x27;Survey deleted&#x27; });\n  });\n});<p>&#x2F;&#x2F; \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u043f\u0440\u043e\u0441\u044b\nrouter.get(&#x27;&#x2F;&#x27;, (req, res) =&gt; {\n  Survey.getAll((err, results) =&gt; {\n    if (err) return res.status(500).json(err);\n    res.json(results);\n  });\n});<p>module.exports = router;\n```<p>#### 5. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 \u0444\u0430\u0439\u043b (`server.js`)<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0440\u043e\u0443\u0442\u043e\u0432.<p>```javascript\n&#x2F;&#x2F; server.js\nconst express = require(&#x27;express&#x27;);\nconst app = express();\nconst surveyRoutes = require(&#x27;.&#x2F;routes&#x2F;surveyRoutes&#x27;);<p>app.use(express.json());\napp.use(&#x27;&#x2F;api&#x2F;surveys&#x27;, surveyRoutes);<p>const PORT = 3000;\napp.listen(PORT, () =&gt; {\n  console.log(`Server is running on port ${PORT}`);\n});\n```<p>#### 6. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b `surveys` \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<p>```sql\nCREATE TABLE surveys (\n  id INT AUTO_INCREMENT PRIMARY KEY,\n  title VARCHAR(255) NOT NULL,\n  description TEXT,\n  editable BOOLEAN DEFAULT TRUE\n);\n```<p>### \u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0430<p>1. \u0423\u0431\u0435\u0434\u0438\u0441\u044c, \u0447\u0442\u043e MySQL \u0441\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u0438 \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u0430.\n2. \u0417\u0430\u043f\u0443\u0441\u0442\u0438 \u0441\u0435\u0440\u0432\u0435\u0440:<p>```bash\nnode server.js\n```<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0442\u044b \u043c\u043e\u0436\u0435\u0448\u044c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c API, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u043e\u0434\u0435 Postman \u0438\u043b\u0438 curl \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443:<p>- `POST &#x2F;api&#x2F;surveys&#x2F;create` \u2014 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043e\u043f\u0440\u043e\u0441.\n- `PUT &#x2F;api&#x2F;surveys&#x2F;update&#x2F;:id` \u2014 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u043f\u0440\u043e\u0441 (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437).\n- `DELETE &#x2F;api&#x2F;surveys&#x2F;delete&#x2F;:id` \u2014 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u043f\u0440\u043e\u0441.\n- `GET &#x2F;api&#x2F;surveys` \u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u043f\u0440\u043e\u0441\u044b.<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u0442\u0432\u043e\u0438 \u0437\u0430\u0434\u0430\u0447\u0438. \u0423\u0434\u0430\u0447\u0438 \u043d\u0430 \u0441\u043e\u0440\u0435\u0432\u043d\u043e\u0432\u0430\u043d\u0438\u044f\u0445!","created_at":"2024-11-16T03:02:39Z","created_at_i":1731726159,"objectID":"42153769","parent_id":42127881,"story_id":42127881,"story_title":"Show HN: Wewod \u2013 A Social Score Tracker for CrossFit WODs","story_url":"https://wewod.app/","updated_at":"2026-03-29T18:07:32Z"},{"_highlightResult":{"author":{"matchLevel":"none","matchedWords":[],"value":"fghsss"},"comment_text":{"fullyHighlighted":false,"matchLevel":"full","matchedWords":["sql","\u0437\u0430\u043f\u0440\u043e\u0441"],"value":"\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043e\u043f\u0440\u043e\u0441\u0430, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u0430\u0434\u043c\u0438\u043d\u0430 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL. \u041d\u0430\u0447\u043d\u0435\u043c \u0441 MySQL \u0438 \u0434\u0430\u043c\u043f\u0430 \u0431\u0430\u0437\u044b, \u0437\u0430\u0442\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433\u0438\u043a\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443.<p>### \u0428\u0430\u0433 1: \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 MySQL \u0438 \u0434\u0430\u043c\u043f\u0430<p>1. \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 `surveys`.<p>```<em>sql</em>\nCREATE DATABASE survey_db;\nUSE survey_db;<p>CREATE TABLE surveys (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    title VARCHAR(255) NOT NULL,\n    editable BOOLEAN DEFAULT TRUE,\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);<p>CREATE TABLE responses (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    survey_id INT,\n    response_text TEXT,\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n    FOREIGN KEY (survey_id) REFERENCES surveys(id)\n);\n```<p>2. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u0430\u043c\u043f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u0443:<p>```bash\nmysqldump -u your_username -p survey_db &gt; survey_db_dump.<em>sql</em>\n```<p>### \u0428\u0430\u0433 2: \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0441 MySQL \u0432 `app.js`<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u043c\u043e\u0434\u0443\u043b\u044c `mysql2`:<p>```bash\nnpm install mysql2\n```<p>\u0412 `app.js` \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445:<p>```javascript\nconst mysql = require('mysql2');<p>// \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445\nconst db = mysql.createConnection({\n  host: 'localhost',\n  user: 'your_username',\n  password: 'your_password',\n  database: 'survey_db'\n});<p>db.connect(err =&gt; {\n  if (err) throw err;\n  console.log('Connected to MySQL database');\n});\n```<p>### \u0428\u0430\u0433 3: \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u0430\u0434\u043c\u0438\u043d\u0430<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0447\u0435\u0440\u0435\u0437 JWT. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 `jsonwebtoken`:<p>```bash\nnpm install jsonwebtoken\n```<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0441\u0435\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447:<p>```javascript\nconst jwt = require('jsonwebtoken');\nconst SECRET_KEY = 'your_secret_key';\n```<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 middleware \u0434\u043b\u044f \u0437\u0430\u0449\u0438\u0442\u044b \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u0432:<p>```javascript\n// \u042d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u043d\u0430\napp.post('/login', (req, res) =&gt; {\n  const { username, password } = req.body;\n  if (username === 'admin' &amp;&amp; password === 'password') { // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435\n    const token = jwt.sign({ username }, SECRET_KEY, { expiresIn: '1h' });\n    res.json({ token });\n  } else {\n    res.status(401).json({ error: 'Invalid credentials' });\n  }\n});<p>// Middleware \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0442\u043e\u043a\u0435\u043d\u0430\nconst authenticateToken = (req, res, next) =&gt; {\n  const token = req.headers['authorization'];\n  if (!token) return res.sendStatus(401);<p><pre><code>  jwt.verify(token, SECRET_KEY, (err, user) =&gt; {\n    if (err) return res.sendStatus(403);\n    req.user = user;\n    next();\n  });</code></pre>\n};\n```<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 `authenticateToken` \u043a\u0430\u043a middleware \u043a \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u044b\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<p>```javascript\napp.post('/surveys', authenticateToken, (req, res) =&gt; { /* \u043a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u043f\u0440\u043e\u0441\u0430 <i>/ });\n```<p>### \u0428\u0430\u0433 4: \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043e\u043f\u0440\u043e\u0441\u0430<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0438. \u042d\u0442\u043e\u0442 \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u043f\u0440\u043e\u0441\u0430.<p>```javascript\napp.get('/surveys/:id/statistics', authenticateToken, (req, res) =&gt; {\n  const surveyId = req.params.id;<p><pre><code>  db.query('SELECT COUNT(*) AS response_count FROM responses WHERE survey_id = ?', [surveyId], (err, results) =&gt; {\n    if (err) throw err;\n    res.json({ surveyId, response_count: results[0].response_count });\n  });</code></pre>\n});\n```<p>### \u041f\u043e\u043b\u043d\u044b\u0439 `app.js`<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442:\n1. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e JWT.\n2. \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL.\n3. \u0421\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0430 \u043e\u043f\u0440\u043e\u0441\u044b.<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044f <em>\u0437\u0430\u043f\u0440\u043e\u0441</em>\u044b \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440.</i>"},"story_title":{"matchLevel":"none","matchedWords":[],"value":"Show HN: Wewod \u2013 A Social Score Tracker for CrossFit WODs"},"story_url":{"matchLevel":"none","matchedWords":[],"value":"https://wewod.app/"}},"_tags":["comment","author_fghsss","story_42127881"],"author":"fghsss","comment_text":"\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043e\u043f\u0440\u043e\u0441\u0430, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u0430\u0434\u043c\u0438\u043d\u0430 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL. \u041d\u0430\u0447\u043d\u0435\u043c \u0441 MySQL \u0438 \u0434\u0430\u043c\u043f\u0430 \u0431\u0430\u0437\u044b, \u0437\u0430\u0442\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433\u0438\u043a\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443.<p>### \u0428\u0430\u0433 1: \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 MySQL \u0438 \u0434\u0430\u043c\u043f\u0430<p>1. \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 `surveys`.<p>```sql\nCREATE DATABASE survey_db;\nUSE survey_db;<p>CREATE TABLE surveys (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    title VARCHAR(255) NOT NULL,\n    editable BOOLEAN DEFAULT TRUE,\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);<p>CREATE TABLE responses (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    survey_id INT,\n    response_text TEXT,\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n    FOREIGN KEY (survey_id) REFERENCES surveys(id)\n);\n```<p>2. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u0430\u043c\u043f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u0443:<p>```bash\nmysqldump -u your_username -p survey_db &gt; survey_db_dump.sql\n```<p>### \u0428\u0430\u0433 2: \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0441 MySQL \u0432 `app.js`<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u043c\u043e\u0434\u0443\u043b\u044c `mysql2`:<p>```bash\nnpm install mysql2\n```<p>\u0412 `app.js` \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445:<p>```javascript\nconst mysql = require(&#x27;mysql2&#x27;);<p>&#x2F;&#x2F; \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445\nconst db = mysql.createConnection({\n  host: &#x27;localhost&#x27;,\n  user: &#x27;your_username&#x27;,\n  password: &#x27;your_password&#x27;,\n  database: &#x27;survey_db&#x27;\n});<p>db.connect(err =&gt; {\n  if (err) throw err;\n  console.log(&#x27;Connected to MySQL database&#x27;);\n});\n```<p>### \u0428\u0430\u0433 3: \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u0430\u0434\u043c\u0438\u043d\u0430<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0447\u0435\u0440\u0435\u0437 JWT. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 `jsonwebtoken`:<p>```bash\nnpm install jsonwebtoken\n```<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0441\u0435\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447:<p>```javascript\nconst jwt = require(&#x27;jsonwebtoken&#x27;);\nconst SECRET_KEY = &#x27;your_secret_key&#x27;;\n```<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 middleware \u0434\u043b\u044f \u0437\u0430\u0449\u0438\u0442\u044b \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u0432:<p>```javascript\n&#x2F;&#x2F; \u042d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u043d\u0430\napp.post(&#x27;&#x2F;login&#x27;, (req, res) =&gt; {\n  const { username, password } = req.body;\n  if (username === &#x27;admin&#x27; &amp;&amp; password === &#x27;password&#x27;) { &#x2F;&#x2F; \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435\n    const token = jwt.sign({ username }, SECRET_KEY, { expiresIn: &#x27;1h&#x27; });\n    res.json({ token });\n  } else {\n    res.status(401).json({ error: &#x27;Invalid credentials&#x27; });\n  }\n});<p>&#x2F;&#x2F; Middleware \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0442\u043e\u043a\u0435\u043d\u0430\nconst authenticateToken = (req, res, next) =&gt; {\n  const token = req.headers[&#x27;authorization&#x27;];\n  if (!token) return res.sendStatus(401);<p><pre><code>  jwt.verify(token, SECRET_KEY, (err, user) =&gt; {\n    if (err) return res.sendStatus(403);\n    req.user = user;\n    next();\n  });</code></pre>\n};\n```<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 `authenticateToken` \u043a\u0430\u043a middleware \u043a \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u044b\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<p>```javascript\napp.post(&#x27;&#x2F;surveys&#x27;, authenticateToken, (req, res) =&gt; { &#x2F;* \u043a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u043f\u0440\u043e\u0441\u0430 <i>&#x2F; });\n```<p>### \u0428\u0430\u0433 4: \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043e\u043f\u0440\u043e\u0441\u0430<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0438. \u042d\u0442\u043e\u0442 \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u043f\u0440\u043e\u0441\u0430.<p>```javascript\napp.get(&#x27;&#x2F;surveys&#x2F;:id&#x2F;statistics&#x27;, authenticateToken, (req, res) =&gt; {\n  const surveyId = req.params.id;<p><pre><code>  db.query(&#x27;SELECT COUNT(*) AS response_count FROM responses WHERE survey_id = ?&#x27;, [surveyId], (err, results) =&gt; {\n    if (err) throw err;\n    res.json({ surveyId, response_count: results[0].response_count });\n  });</code></pre>\n});\n```<p>### \u041f\u043e\u043b\u043d\u044b\u0439 `app.js`<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442:\n1. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e JWT.\n2. \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL.\n3. \u0421\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0430 \u043e\u043f\u0440\u043e\u0441\u044b.<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440.</i>","created_at":"2024-11-16T04:10:11Z","created_at_i":1731730211,"objectID":"42154088","parent_id":42127881,"story_id":42127881,"story_title":"Show HN: Wewod \u2013 A Social Score Tracker for CrossFit WODs","story_url":"https://wewod.app/","updated_at":"2024-11-16T05:54:08Z"}],"hitsPerPage":10,"nbHits":2,"nbPages":1,"page":0,"params":"query=sql+%D0%B7%D0%B0%D0%BF%D1%80%D0%BE%D1%81&hitsPerPage=10&advancedSyntax=true&analyticsTags=backend","processingTimeMS":5,"processingTimingsMS":{"_request":{"queue":12,"roundTrip":22},"afterFetch":{"format":{"total":1}},"fetch":{"query":3,"total":4},"total":5},"query":"sql \u0437\u0430\u043f\u0440\u043e\u0441","serverTimeMS":18}
