# This is Node notes
# http模块
const http = require('http'); // 引入http模块
http.createServer(function(req,res) { //创建服务
res.end("hello world"); //服务端输出
}).listen(80);
# express模块
const express = require('express');
const app = express();
app.all("*", function(req, res, next) { //跨域
if (!req.get("Origin")) return next();
// use "*" here to accept any origin
res.set("Access-Control-Allow-Origin", "*");
// res.set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
res.set("Access-Control-Allow-Methods", "GET,POST");
// res.set('Access-Control-Allow-Max-Age', 3600);
if ("OPTIONS" === req.method) return res.send(200);
next();
});
# mongoose模块
const mongoose=require('mongoose'); // 引入模块
const url = `mongodb://127.0.0.1:27017/runoob`; // 数据库地址
mongoose.connect(url,{ // 连接数据库
useNewUrlParser: true,
useCreateIndex: true
});
mongoose.connection.on('connected',() =>{ // 数据库成功回调
console.log('数据库连接成功');
})
# fs模块
const fs = require("fs");
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("异步读取: " + data.toString());
});