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