@ -1,8 +1,8 @@
const express = require ( "express" ) ;
const express = require ( 'express' ) ;
const bodyParser = require ( "body-parser" ) ;
const bodyParser = require ( 'body-parser' ) ;
const cache = require ( "../../netease_api/util/apicache" ) . middleware ;
const cache = require ( '../../netease_api/util/apicache' ) . middleware ;
const fileUpload = require ( "express-fileupload" ) ;
const fileUpload = require ( 'express-fileupload' ) ;
import routes from "../../netease_api/routes" ;
import routes from '../../netease_api/routes' ;
export function startNeteaseMusicApi ( ) {
export function startNeteaseMusicApi ( ) {
// Integrate API
// Integrate API
@ -10,23 +10,23 @@ export function startNeteaseMusicApi() {
// CORS & Preflight request
// CORS & Preflight request
app . use ( ( req , res , next ) => {
app . use ( ( req , res , next ) => {
if ( req . path !== "/" && ! req . path . includes ( "." ) ) {
if ( req . path !== '/' && ! req . path . includes ( '.' ) ) {
res . set ( {
res . set ( {
"Access-Control-Allow-Credentials" : true ,
'Access-Control-Allow-Credentials' : true ,
"Access-Control-Allow-Origin" : req . headers . origin || "*" ,
'Access-Control-Allow-Origin' : req . headers . origin || '*' ,
"Access-Control-Allow-Headers" : "X-Requested-With,Content-Type" ,
'Access-Control-Allow-Headers' : 'X-Requested-With,Content-Type' ,
"Access-Control-Allow-Methods" : "PUT,POST,GET,DELETE,OPTIONS" ,
'Access-Control-Allow-Methods' : 'PUT,POST,GET,DELETE,OPTIONS' ,
"Content-Type" : "application/json; charset=utf-8" ,
'Content-Type' : 'application/json; charset=utf-8' ,
} ) ;
} ) ;
}
}
req . method === "OPTIONS" ? res . status ( 204 ) . end ( ) : next ( ) ;
req . method === 'OPTIONS' ? res . status ( 204 ) . end ( ) : next ( ) ;
} ) ;
} ) ;
// cookie parser
// cookie parser
app . use ( ( req , res , next ) => {
app . use ( ( req , res , next ) => {
req . cookies = { } ;
req . cookies = { } ;
( req . headers . cookie || "" ) . split ( /\s*;\s*/ ) . forEach ( ( pair ) => {
( req . headers . cookie || '' ) . split ( /\s*;\s*/ ) . forEach ( pair => {
let crack = pair . indexOf ( "=" ) ;
let crack = pair . indexOf ( '=' ) ;
if ( crack < 1 || crack == pair . length - 1 ) return ;
if ( crack < 1 || crack == pair . length - 1 ) return ;
req . cookies [
req . cookies [
decodeURIComponent ( pair . slice ( 0 , crack ) ) . trim ( )
decodeURIComponent ( pair . slice ( 0 , crack ) ) . trim ( )
@ -42,17 +42,17 @@ export function startNeteaseMusicApi() {
app . use ( fileUpload ( ) ) ;
app . use ( fileUpload ( ) ) ;
// cache
// cache
app . use ( cache ( "2 minutes" , ( req , res ) => res . statusCode === 200 ) ) ;
app . use ( cache ( '2 minutes' , ( req , res ) => res . statusCode === 200 ) ) ;
// router
// router
Object . keys ( routes ) . forEach ( ( route ) => {
Object . keys ( routes ) . forEach ( route => {
app . use ( route , routes [ route ] ) ;
app . use ( route , routes [ route ] ) ;
} ) ;
} ) ;
const port = process . env . PORT || 10754 ;
const port = process . env . PORT || 10754 ;
const host = process . env . HOST || "127.0.0.1" ;
const host = process . env . HOST || '127.0.0.1' ;
app . server = app . listen ( port , host , ( ) => {
app . server = app . listen ( port , host , ( ) => {
console . log ( ` server running @ http:// ${ host ? host : "localhost" } : ${ port } ` ) ;
console . log ( ` server running @ http:// ${ host ? host : 'localhost' } : ${ port } ` ) ;
} ) ;
} ) ;
}
}