express与mongoose基本使用,以及cors解决跨域

//express使用
const express = require('express')
const app = express()

app.listen(3001, () => {
    console.log("http://localhost:3001")
})

app.use(express.json())

app.get('/', (req, res) => {
    console.log("hello express!")
})

//cors
app.use(require('cors')())  //中间件引入cors并使用,解决跨域问题

//mongoose使用
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/db_name', { //db_name可以随便起名字
    useNewUrlParser: true
})
const model = new mongoose.Schema({
    title: { type: String },
    body: { type: String }
})
const Model = mongoose.model('Model', model)

 node test.js之后

猜你喜欢

转载自www.cnblogs.com/gehaoyu/p/11922468.html