Files
xiezheng/app.js
编码猿 4dd0c5d928
Some checks reported errors
continuous-integration/drone/push Build was killed
增加接口,增加index
2024-12-12 11:10:07 +08:00

67 lines
2.0 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import express from "express";
import config from "./config/config.js";
import { ipv6, ipv4 } from "./utils/ip.js";
import tagsRouter from "./router/tags.js";
import infoRouter from "./router/info.js";
import searchRouter from "./router/search.js";
import homeRouter from "./router/home.js";
import hotRouter from "./router/hot.js";
import http from "http";
const app = express()
//解决跨域
app.use((req, res, next) => {
// 设置是否运行客户端设置 withCredentials
// 即在不同域名下发出的请求也可以携带 cookie
res.header("Access-Control-Allow-Credentials", true)
// 第二个参数表示允许跨域的域名,* 代表所有域名
res.header('Access-Control-Allow-Origin', '*')//配置80端口跨域
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, OPTIONS') // 允许的 http 请求的方法
// 允许前台获得的除 Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma 这几张基本响应头之外的响应头
res.header('Access-Control-Allow-Headers', '*')
if (req.method == 'OPTIONS') {
res.sendStatus(200)
} else {
next()
}
})
app.use('/v1/api/tag', tagsRouter);
app.use('/v1/api/info', infoRouter);
app.use('/v1/api/search', searchRouter);
app.use('/v1/api/home', homeRouter);
app.use('/v1/api/hot', hotRouter);
http.createServer(app).listen(config.port, "::",() => {
console.log(`
爬虫运行在下面网址:
1⃣ 公网 IPV6: http://[${ipv6()}]:${config.port}
2⃣ 局域网 IPV4: http://${ipv4()}:${config.port}
`)
})
// error handler
app.use(function(err, req, res, next) {
console.log(`
程序出现错误
status信息为${err.status}
message信息为${err.message}
error为${err}
`)
});
// app.listen(config.port, () => {
// console.log(`
// 爬虫运行在下面网址:
// 1⃣ 公网 IPV6: http://[${ipv6()}]:${config.port}
// 2⃣ 局域网 IPV4: http://${ipv4()}:${config.port}
// `)
// })
export default app