Node.jsのコード
-
JAVASCRIPT Node.jsでコマンドラインの引数管理をするconst commandLineArgs = require('command-line-args'); const optionDefinitions = [ { name: 'verbose', alias: 'v', type: Boolean }, { name: 'title', alias: 't', type: String, }, { name: 'num', alias: 'n', type: Number, defaultValue: 3 } ]; const options = commandLineArgs(optionDefinitions);
-
JAVASCRIPT MongoDBでfindで絞り込みをして一括アップデート(Node.js)const collection = await db.collection('user') const tasks = await collection.updateMany( { "alive": true, "last_login": { $lte: dayjs().subtract(1, 'year').toISOString() } }, { $set: { "alive": false }, } )
-
JAVASCRIPT Javascriptでメソッド名やプロパティ名を動的に呼び出すconst name = 'hoge' let method_name = 'mtd_' + name let property_name = 'prop_' + name await SomeFunc[method_name](); // メソッド await SomeFunc[property_name]; // プロパティ
-
DOCKER Node.js + yarnのDockerイメージのDockerfileDockerfile FROM node:12.6-alpine WORKDIR /app RUN apk update && \ apk add git && \ apk add --no-cache curl && \ curl -o- -L https://yarnpkg.com/install.sh | sh ENV PATH $HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH CMD ["sh"]
-
JAVASCRIPT node.js ExpressでXMLフォーマットを出力する// // 何かのコード // res.type('application/xml'); return res.send(sitemap)
-
JAVASCRIPT Expressでキャッシュコントロールする「express-cache-controller」の使い方const cacheControl = require('express-cache-controller'); app.use(cacheControl({ maxAge: 0 })); app.get('/prefecture/:id/detail.json' function(req, res, next) { // 何かの処理 res.cacheControl = { maxAge: 15552000 }; return res.send(何かのデータ) });