MongoDB的模糊查询可以用正则表达式来查询,比如我想查找文章中是否有某段话,如下:
db.getCollection('posts').find({'content.content':{$regex:'http:\/\/localhost'}})
需要注意的点就是转义,如果是子对象就用.来表示,但是要加引号。附数据库结构:
由于浏览器著名的同源策略
的限制。当需要访问不同源的服务时,需要服务端允许跨域访问。下面是一个例子:
//server.js
const http = require('http');
const fs = require('fs');
http.createServer((req, res) => {
console.log('request come', req.url);
const html = fs.readFileSync('test.html', 'utf8');
res.writeHead(200, {
'Content-Type': 'text/html'
})
res.end(html);
}).listen(8888, () => {
console.log('listened on port 8888');
})
//server2.js
const http = require('http');
http.createServer((req, res) => {
console.log('request come', req.url);
res.writeHead(200, {
'Access-Control-Allow-Origin': '*'//不安全。允许所有
'Access-Control-Allow-Origin': 'http://baidu.com' //只允许特定的服务跨域
})
res.end('123');
}).listen(8887)