require

Include PHP PHAR without .phar extension

孤街浪徒 提交于 2020-01-24 22:23:04
问题 I'm having trouble including a PHAR into my application bootstrapping code. I'm attempting to include the phpunit/phpunit PHAR to have the PHPUnit classes available in my application. Given I have the following directory structure: /path/to/code/ app.php phpunit Where phpunit is the PHPUnit as a PHAR, and app.php contains: <?php $phar = 'phar://' . __DIR__ . '/phpunit'; include $phar; assert(class_exists('\\PHPUnit\\Framework\\TestCase')); I get the following error: PHP Warning: include(phar:

vue的服务器端渲染

余生长醉 提交于 2020-01-24 20:42:49
0. 服务端渲染简介 服务端渲染不是一个新的技术;在 Web 最初的时候,页面就是通过服务端渲染来返回的,用 PHP 来说,通常是使用 Smarty 等模板写模板文件,然后 PHP 服务端框架将数据和模板渲染为页面返回,这样的服务端渲染有个缺点就是一旦要查看新的页面,就需要请求服务端,刷新页面。 但如今的前端,为了追求一些体验上的优化,通常整个渲染在浏览器端使用 JS 来完成,配合 history.pushState 等方式来做单页应用(SPA: Single-Page Application),也收到不错的效果,但是这样还是有一些缺点:第一次加载过慢,用户需要等待较长时间来等待浏览器端渲染完成;对搜索引擎爬虫等不友好。这时候就出现了类似于 React,Vue 2.0 等前端框架来做服务端渲染。 使用这些框架来做服务端渲染的兼顾了上面的几个优点,而且写一份代码就可以跑在服务端和浏览器端。Vue 2.0 发布了也有一段时间了,新版本比较大的更新就是支持服务端渲染,最近有空折腾了下 Vue 的服务端渲染,记录下来。 1. 在 Vue 2.0 中使用服务端渲染 官方文档给了一个简单的例子来做服务端渲染: // 步骤 1:创建一个Vue实例 var Vue = require('vue') var app = new Vue({ render: function (h) { return

NodeJS require路径

半世苍凉 提交于 2020-01-24 14:36:52
项目需要用nodejs,感觉nodejs是前端装逼神器了,是通向全栈工程师的必经之路哇,接下来开始踏上学习nodejs的征程。下面是第一个hello,world的程序。 1、server.js文件,这相当于服务器脚本。 var http = require("http"); function start() { function onRequest(request, response) { console.log("Request recieved") response.writeHead(200, { "Content-Type": "text/plain" }); response.write("hello,world"); response.end(); } http.createServer(onRequest).listen(8888); } exports.start=start; 这是最简单的一个模块,http是nodejs自带的模块,start是自己定义的一个模块。 2、index.js。这是执行文件,注意require的路径。 var server=require("./module/server"); server.start(); 在项目目录下用node运行node index.js,然后在浏览器中输入:http://localhost

What is the difference between require() and new require()?

偶尔善良 提交于 2020-01-24 10:49:26
问题 The core of my question is, what's the difference between var fs = new require('fs'); and var fs = require('fs'); Are there any effects or caveats if I were to use new for all modules everywhere? While using Webstorm, I noticed that I can get intellisense working only if I use new require('fs') . Before I start using it consistently for a better development experience, I wanted to know a bit more about it. 回答1: First of all: Do not confuse new require("...") (which invokes require as a

What is the difference between require() and new require()?

拟墨画扇 提交于 2020-01-24 10:49:08
问题 The core of my question is, what's the difference between var fs = new require('fs'); and var fs = require('fs'); Are there any effects or caveats if I were to use new for all modules everywhere? While using Webstorm, I noticed that I can get intellisense working only if I use new require('fs') . Before I start using it consistently for a better development experience, I wanted to know a bit more about it. 回答1: First of all: Do not confuse new require("...") (which invokes require as a

NodeJS中的module.exports、require和ES6中的export、import区别

筅森魡賤 提交于 2020-01-24 07:36:13
ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使用module.exports导出接口。 nodejs模块中exports和module.exports的区别 CommonJS模块规范 对于node执行的每一个js文件,都会自动创建一个module模块对象,如同一个封闭的函数一样,把一个函数封闭起来,外部怎样才能去引用呢?必然需要对完暴露出来一个“门”,也就是暴露出来一个接口。。 exports 和 module.exports 二者的关系: 1.module.exports才是真正的接口,exports只不过是它的一个辅助工具, 最终返回给调用的是module.exports而不是exports。module.exports 初始值为一个空对象 {},而exports为指向module.exports 的引用 2. 在require() 的时候,返回的是 module.exports 而不是 exports,因此,直接赋值exports常常会出现错误,而赋值为module.exports常常是解决这一问题的折中办法 3.所有的exports收集到的属性和方法,都赋值给了Module.exports。当然,这有个前提

Node笔记(2)

元气小坏坏 提交于 2020-01-24 05:18:39
写一个可以生成多层级文件夹的函数 const fs = require('fs'); const path = require('path'); function mkdirs (pathname,callback){ var root = path.dirname(module.parent.filename); pathname = path.isAbsolute(pathname)?pathname:path.join(__dirname,pathname); relativepath = path.relative(__dirname,pathname); var folders = relativepath.split(path.sep); try { var pre = ''; folders.forEach(folder =>{ try { //如果不存在则报错 fs.statSync(path.join(__dirname,pre,folder)); } catch (error) { fs.mkdirSync(path.join(__dirname,pre,folder)); } pre = path.join(pre, folder); }); callback && callback(null); } catch (error) { callback &&

express中间件

不打扰是莪最后的温柔 提交于 2020-01-23 23:22:00
加载静态资源--复习以前学的express express怎么用? 如何获取请求? 如何处理响应? 如何对向外暴露静态资源? express核心:中间件:如何理解? 中间件:用来处理 http 请求的一个具体的环节(可能要执行某个具体的处理函数) 中间件一般都是通过修改 req 或者 res 对象来为后续的处理提供便利的使用 中间件分类: use(function () {req, res, next}) 不关心请求方法和请求路径,没有具体路由规则,任何请求都会进入该中间件 use('请求路径', function (req, res, next) {}) 不关心请求方法,只关心请求路劲的中间件 get('请求路径', function (req, res, next) {}) 具体路由规则中间件 post('请求路径', function (req, res, next) {}) // 需求一:用户访问 / 响应 hello world // 需求二:用户访问 /login 响应 hello login // 需求三:将 public 目录开放为类似于 Apache 一样,可以直接通过路径去取访问该目录中的任意资源 const express = require('express') const fs = require('fs') // 1. 调用 express() 方法

Node.JS 模块,包管理与开发

蓝咒 提交于 2020-01-23 14:29:55
谈到组件,JavaScript是不具备这个特点的,以前JavaScript很多功能都依赖不同厂商的实现。基本在组件这个方面有如下特点: .JavaScript没有模块系统。没有原生的支持密闭作用域或依赖管理。 .JavaScript没有标准库。核心库外,没有文件系统的API等,不过Html5规范出来后,这些将会有的:)。 .JavaScript没有标准API接口。 .JavaScript没有包管理系统。 当有了CommonJS( http://www.commonjs.org )规范的出现,其目标是为了构建JavaScript在包括Web服务器,桌面,命令行工具,及浏览器方面的生态系统。 CommonJS制定了解决这些问题的一些规范,而Node.js就是这些规范的一种实现。Node.js自身实现了require方法作为其引入模块的方法,同时NPM也基于CommonJS定义的包规范,实现了依赖管理和模块自动安装等功能。这里我们将深入一下Node.js的require机制和NPM基于包规范的应用。 一,Node.JS模块包管理: 1.编写及加载使用模块 在Node.JS中编写及安装一个js的模块是很简单的。 首先写编写一个test.js,内容如下: exports.myfunc= function () {   console.log('hello,world'); }; 也可以写成:

Mongoose 数据校验

泪湿孤枕 提交于 2020-01-23 13:58:40
什么是mongoose数据校验 用户通过mongoose给mongodb数据库增加数据的时候,对数据的合法性进行的验证 mongoose里面定义Schema:字段类型,修饰符、默认参数 、数据校验都是为了数据库数据的一致性 Schema,为数据库对象的集合,每个schema会映射到mongodb中的一个collection,定义Schema可以理解为表结构的定义 Mongoose内置的校验参数 代码演示,首先还是有个db.js(用于连接数据库)和users.js(操作users集合的schema模块)还有app.js const mongoose = require('mongoose'); mongoose.connect('mongodb://127.0.0.1:27017/eggcms', { useNewUrlParser: true }, (err) => { if(err){ return console.log(err); } console.log('数据库连接成功') }); module.exports = mongoose let mongoose = require('./db') let UserSchema = mongoose.Schema({ name: { type: String }, age: { type: Number }, status: