惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
P
Privacy & Cybersecurity Law Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
C
Cybersecurity and Infrastructure Security Agency CISA
S
Security Affairs
Latest news
Latest news
Security Latest
Security Latest
N
News and Events Feed by Topic
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
The Exploit Database - CXSecurity.com
The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Vulnerabilities – Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
博客园_首页
S
Secure Thoughts
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AWS News Blog
AWS News Blog
腾讯CDC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
N
News and Events Feed by Topic
A
Arctic Wolf
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
Project Zero
Project Zero
A
About on SuperTechFans
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Know Your Adversary
Know Your Adversary
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
K
Kaspersky official blog
L
LINUX DO - 最新话题
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs

oida.dev | TypeScript, Rust

TypeScript's `erasableSyntaxOnly` Flag Unsafe for work Tokio: Macros Tokio: Channels Tokio: Getting Started Network Applications on the Tokio Stack Remake, Remodel, Reduce. The `never` type and error handling in TypeScript 5 Inconvenient Truths about TypeScript Refactoring in Rust: Introducing Traits Refactoring in Rust: Abstraction with the Newtype Pattern Announcing the TypeScript Cookbook TypeScript: Iterating over objects The road to universal JavaScript 10 years of oida.dev Rust: Tiny little traits The TypeScript converging point How not to learn TypeScript Getting started with Rust Introducing Slides and Coverage TypeScript: The humble function overload TypeScript + React: Children types are broken TypeScript: In defense of any Rust: Enums to wrap multiple errors Dissecting Deno Error handling in Rust TypeScript: Unexpected intersections Upgrading Node.js dependencies after a yarn audit TypeScript: Array.includes on narrow types TypeScript + React: Typing Generic forwardRefs shared, util, core: Schroedinger's module names Learning Rust and Go TypeScript: Narrow types in catch clauses TypeScript: Low maintenance types Tidy TypeScript: Name your generics Tidy TypeScript: Avoid traditional OOP patterns Tidy TypeScript: Prefer type aliases over interfaces Tidy TypeScript: Prefer union types over enums My new book: TypeScript in 50 Lessons Go Preact! ❤️ this in JavaScript and TypeScript TypeScript and ECMAScript Modules TypeScript + React: Why I don't use React.FC TypeScript + React: Component patterns TypeScript: Augmenting global and lib.dom.d.ts Vite with Preact and TypeScript TypeScript: Union to intersection type 11ty: Generate Twitter cards automatically Are large node module dependencies an issue? TypeScript: Variadic Tuple Types Preview TypeScript: Improving Object.keys Remake, Remodel. Part 4. TypeScript + React: Typing custom hooks with tuple types TypeScript: Assertion signatures and Object.defineProperty TypeScript: Check for object properties and narrow down type Boolean in JavaScript and TypeScript void in JavaScript and TypeScript Symbols in JavaScript and TypeScript Why I use TypeScript TypeScript + React: Extending JSX Elements TypeScript: Validate mapped types and const context TypeScript: Match the exact object shape TypeScript: The constructor interface pattern Streaming your Meetup - Part 4: Directing and Streaming with OBS Streaming your Meetup - Part 3: Speaker audio Streaming your Meetup - Part 2: Speaker video Streaming your Meetup - Part 1: Basics and Projector TypeScript and React Guide: Added a new styles chapter TypeScript and React Guide: Added a new render props chapter TypeScript and React: Styles and CSS TypeScript and React TypeScript and React Guide: Added a new prop types chapter TypeScript without TypeScript -- JSDoc superpowers TypeScript: Mapped types for type maps JAMStack vs serverless web apps The Unsung Benefits of JAMStack Sites TypeScript: Ambient modules for Webpack loaders My most favourite talks in 2018 TypeScript and React Guide: Added a new context chapter TypeScript: Built-in generic types TypeScript: Type predicates JSX is syntactic sugar TypeScript and React Guide: Added a new hooks chapter Getting your CfP application right FAQ on our Angular Connect Talk: Automating UI development TypeScript and Substitutability Debugging Node.js apps in TypeScript with Visual Studio Code From Medium: Deconfusing Pre- and Post-processing From Medium: PostCSS misconceptions Saving and scraping a website with Puppeteer Cutting the mustard - 2018 edition Wordpress as CMS for your JAMStack sites My most favourite podcast episodes in 2017 My most favourite talks in 2017 My most favourite books in 2017 The Best Request Is No Request, Revisited Not so hidden figures - Organizing ScriptConf My podcast journey to ScriptCast Grid layout, grid layout everywhere! #scriptconf and #devone
Running an on-demand PHP server with BrowserSync and Grunt/Gulp
2015-03-21 · via oida.dev | TypeScript, Rust

Quite a while ago I wrote a little article on connect middleware and how to run PHP with it. While the article was originally intended to introduce the concept of connect middlewares to the Grunt audience, I get a lot of feedback on the PHP part. Which was actually broken by design. So, if you’re search for a real on-demand PHP server in your Grunt or Gulp setup, and have all the livereload goodness you know from your connect server, proceed:

Starting a real PHP server #

The problem with the original solution was, that it tried to fit in one server (a PHP server) into the other (connect), which isn’t possible at all. What was possible though, was to execute the PHP process every time a PHP file turns up in our connect stream. This would worked with basic includes, programming constructs, and $_GET parameters, however all the server stuff wouldn’t. What we need is a real PHP server.

Since PHP 5.4 you have the possibility to run an on-demand web server everywhere on your command line by simply typing php -S and the server address you want to have it listen to, like php -S localhost:8000. This is also not intended to replace a web server with PHP functionality, but serves quite well for development reasons. Now we just need a way to run it in Node.js, and better: In one of our build systems. Good for us there’s the ever-coding Sindre Sorhus who offers Grunt-PHP for us. Install it to your project with npm install --save-dev grunt-php.

Setup is rather easy if you’re familiar with connect:

grunt.initConfig({
php: {
test: {
options: {
base: 'app',
port: 8010,
keepalive: true,
open: true
}
}
}
});

grunt.registerTask('server', ['php'])

This snippet opens up a PHP server running on localhost and port 8010, the open property calls the nearest browser to open, and keepalive tells our Grunt not to stop after executing the task.

You can do the same with Gulp. There’s a plugin out there called gulp-connect-php, which is the most misleading name for a node module since you neither have to have Gulp for that one nor does it has anything to do with connect (so now tell me that the Grunt plugin directory is convoluted!). Anyhow, if you want to use it with Gulp, install it and start it that way:

var gulp = require('gulp'),
php = require('gulp-connect-php');

gulp.task('php', function() {
php.server({ base: 'app', port: 8010, keepalive: true});
});

That’s basically all you need, you can go and enjoy your PHP server, started from your build files.

Adding BrowserSync as a Livereload replacement #

As the documentation in “Grunt-PHP” states: There is no way for middleware like there was in grunt-contrib-connect. That’s mainly because the middleware concept is a thing of connect, not PHP. But we still want to use LiveReload (Getting all your results without having to refresh your browser is a real performance booster), and maybe some other middlewares we are used to. This is where BrowserSync comes in. BrowserSync essentially is already a connect+livereload+custom middleware setup. But bundled together in one package without custom setup in a pure Node script and with command line tools to lower some barriers. One of the features which intrigued me the most was the possibility to let BrowserSync create a proxy for another server.

So BrowserSync forwards all requests to some other server, like our newly created PHP server, and when responding to it, it includes all the necessary scripts for livereloading and such.

For Grunt, this setup looks like this:

'use strict';

module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-browser-sync');
grunt.loadNpmTasks('grunt-php');
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.initConfig({
watch: {
php: {
files: ['app/**/*.php']
}
},
browserSync: {
dev: {
bsFiles: {
src: 'app/**/*.php'
},
options: {
proxy: '127.0.0.1:8010', //our PHP server
port: 8080, // our new port
open: true,
watchTask: true
}
}
},
php: {
dev: {
options: {
port: 8010,
base: 'app'
}
}
}
});

grunt.registerTask('default', ['php', 'browserSync', 'watch']);
};

Take a look at the browserSync task: We told him which files to watch for reloading (the bsFiles property), and to forward all calls from localhost:8080 to 127.0.0.1:8010 with the proxy attribute. Note also that I added an (kinda empty) watch task to make sure that our server doesn’t smoke off after one run, and that I removed the open and keepAlive properties. This way it’s more suited for your other Grunt setup.

In Gulp, our code’s even less. And uses actually not a single Gulp function whatsoever. We can include browser-sync directly, due to not having the need for wrappers when it’s not meant to run through the Gulp pipeline.

// Gulp 3.8 code... differs in 4.0
var gulp = require('gulp'),
php = require('gulp-connect-php'),
browserSync = require('browser-sync');

var reload = browserSync.reload;

gulp.task('php', function() {
php.server({ base: 'build', port: 8010, keepalive: true});
});
gulp.task('browser-sync',['php'], function() {
browserSync.init({
proxy: '127.0.0.1:8010',
port: 8080,
open: true,
notify: false
});
});
gulp.task('default', ['browser-sync'], function () {
gulp.watch(['build/*.php'], [reload]);
});

The setup has the same changes as the one with Grunt. Note the watch process at the end, which basically tells us to call the reload function of BrowserSync every time a PHP file has changed.

Neat!

Bottom line #

This setup (especially with Gulp) works like a charm and will be included as my last gift for the Yeoman generator I wrote for my previous company. BrowserSync really is a tool which helps with all the clumsy connect setup we had to deal with in our old Grunt/Gulp setups. And with me being all pro “one tool should just do one thing” and having everything split up into manageable, little software parts, I can say I like having the “server thing” done right!

Related Articles