NodeJsのexpressフレーム
4194 ワード
expressダウンロードの作成
//
npm install express express-static --save
//
const express = require("express");
var server = express();
server.listen(8081);
server.use('/', function(req, res){
console.log(req.body);
res.end();
})
express-cookieモジュール // : cookie
// : cookie
//cookie 4k
const experss = require("express");
const cookie = require("cookie-parser");
let server = experss();
server.listen(8081);
server.use(cookie("sdfaj2212"));
server.use("/", function(request, response){
request.secret="aaa";
// cookie
response.cookie("key", "abc123", {signed : true});
// cookie
console.log(request.cookies);
console.log(request.signedCookies);
// cookie
response.clearCookie("key");
response.end();
});
express-sessionモジュール const experss = require("express");
const cookie = require("cookie-parser");
const session = require("cookie-session");
let server = experss();
server.listen(8081);
server.use(cookie("sdfaj2212"));
server.use(session({
name : "sesA",
keys : ['a'],//
maxAge : 1000 * 60 * 20 //cookie
}));
server.use("/", function(request, response){
if(request.session["key"] == null){
request.session["key"] = 1;
} else {
request.session["key"]++;
}
response.end(request.session["key"].toString());
});
サンプルコードimport express from "express";
import consolidate from "consolidate";
import multer from "multer";
import logger from "morgan";
import fs from "fs";
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import cookieSession from 'cookie-session';
import uuid from 'uuid';
import path from "path";
var app = new express();
const port = 8024;
var multerObj = multer({
dest: './public/upload'
});
app.listen(port);
//
app.use(bodyParser.urlencoded({
extended: true,
limit: "500mb"
}));
app.use(bodyParser.json());
app.use(bodyParser.text());
//
app.use(express.static("./public/"));
//
app.use(multerObj.any());
//
app.use(logger("dev"));
app.use(logger("combined", {
stream: fs.createWriteStream("./logs/logs.log", {
flags: 'a'
})
}));
// cookie session
app.use(cookieParser("gongyueqing"));
app.use(cookieSession({
genid: function() {
return uuid.v1();
},
maxAge: 60 * 1000 * 60,
secret: "gongyueqing"
}));
//
app.set("view engine", "html");
app.set("views", "./views/");
app.engine("html", consolidate.ejs);
//
app.use((req, res, next) => {
if (req.url == "/favicon.ico") return;
res.set({
"Content-type": "text/html;charset=utf-8"
});
let admininfo = req.session.admininfo;
let loginpath = req.path;
if (!admininfo && loginpath != "/admin/login" && loginpath != "/admin/login_check") {
return res.redirect("/admin/login");
}
next();
});
//
app.use("/admin", require("./routers/admin/adminRouter"));
//
app.use("/student", require("./routers/student/studentRouter"));
//
app.use("/system", require("./routers/system/systemRouter"));
//
app.use("/client", require("./routers/client/clientRouter"));
// / /
app.use("/medium", require("./routers/medium/mediumRouter"));
// /
app.use("/affair", require("./routers/affair/affairRouter"));
//
app.use("/admin/logoff", (req, res, next) => {
req.session = null;
res.json({
msg: "Log off!",
status: 1
})
next();
});
console.log("http://127.0.0.1:" + port);