?Debian?????Node.js????????????????????????????,??????Node.js???????????
??Node.js
- ???????:
sudo apt-get update
- ??Node.js:
sudo apt-get install nodejs
- ????:
node -v npm -v
????
-
??HTTPS:
- ????HTTPS??????????,???????????????????Let's Encrypt????????
-
????:
- ??Mongoose????????????????????????????
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ username: { type: String, required: true }, email: { type: String, required: true, unique: true }, password: { type: String, required: true } });
-
????:
- ??????????,?????bcrypt???????????????
const bcrypt = require('bcrypt'); const saltRounds = 10; userSchema.pre('save', function(next) { if (!this.isModified('password')) return next(); bcrypt.hash(this.password, saltRounds, (err, hash) => { if (err) return next(err); this.password = hash; next(); }); });
-
??????:
- ????????(DoS)??,????????
express-rate-limit
??????????????
const rateLimit = require("express-rate-limit"); const limiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15 minutes max: 100 // limit each IP to 100 requests per windowMs }); app.use(limiter);
- ????????(DoS)??,????????
-
CORS??:
- ??CORS????????????????API,????????????????
const cors = require('cors'); app.use(cors({ origin: 'https://yourwebsite.com', // ??????????? credentials: true, // ????cookies????? optionsSuccessStatus: 200 // ???????200??? }));
-
????:
- ????????????????????,?????????,????????????????
app.use((err, req, res, next) => { console.error(err.stack); // ????????? res.status(500).send('Something went wrong!'); // ??????????? });
-
????:
- ??????????????,?????????????????????
-
??XSS??:
- ???
helmet
??????????HTTP???????????(XSS)?
const helmet = require('helmet'); app.use(helmet());
- ???
-
??SQL??:
- ??Mongoose?????????,????????????????,???????????????
-
??CSRF??:
- ????????????,?????
csurf
???????????????(CSRF)???
const csurf = require('csurf'); app.use(csurf());
- ????????????,?????
????
- Node.js????
- Node.js?Debian??????Node.js????????
- ???Express?Mongoose?,??????????????Node.js????? ?????
???????????,????Debian???????????Node.js???