Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

server-static

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

server-static

a static file server

  • 2.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
90
increased by11.11%
Maintainers
1
Weekly downloads
 
Created
Source

static-server

静态文件服务器,支持SPA,类似于live-server,支持自定义ajax请求路径mock数据,支持模拟ajax跨域请求进行接口调试

安装

npm install server-static -g

安装完成

可通过

static-server --help 或者 static-server -h

命令查看配置帮助文档

可通过

static-server --port=3001 或者 static-server -p=3001

通过指定监听端口

可通过

static-server --slient 或者 static-server -s

指定不监听文件

可通过

static-server --dir=xxxxx 或者 static-server -d=xxxx

指定在哪个目录下启动服务

也可以通过

static-server

的方式(读取默认配置)启动本服务

配置(如不指定,就用默认配置)

可以通过在项目根目录下新建名为static-server.config.js的文件进行配置

//  static-server.config.js

module.exports = {
	port: 4000,                         // 监听端口,默认3000,当端口被占用时随机
	entry: "index.html",                // 首页文件,及当路径为"/"时响应的页面 
    target: "http://localhost:8080",    // 调试ajax接口的真实地址和前缀
  	slient: true,				      //  是否监听文件
	ignores: [                          //  忽略过滤器(可传入单个函数或者由函数组成的数组)
		function(file) {
		    return /node_modules/.test(file);
		}
	],
	routers: [                          //  自定义请求路由
	    {
	        url: "/login",              //  请求的真实地址
	        method: "GET",
	        cross: true                 //  是否跨域请求
	    },
		{
			url: "/main",                       //  路由
			method: "GET",                      //  请求方式
			handler: function(req, res, next) { //  改路由的处理函数(与connect模块调用方法一致)
				console.log("请求进来了");
				res.statusCode = 200;
				res.end(JSON.stringify({
					res: "test main"
				}));
			}
		},
		{
			url: "/main2",
			method: "POST",
			handler: function(req, res, next) {
				console.log(req.body);
				res.statusCode = 200;
				res.end(JSON.stringify({
					tip: "请求内容",
					res: req.body
				}));
			}
		}
	]
};

在所有接口都需要跨域请求时,可以将static-server.config.js中的routers指定成一个对象,为如下结构即可支持全部跨域调试,更加方便调试

//  static-server.config.js
module.exports = {
	//	...
    routers: {
        url: "*",
        method: "*",
        cross: true
    }
	//	...
};


Keywords

FAQs

Package last updated on 02 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc