Showing posts with the label nodejs

How to set content disposition headers for express nodejs apps

In express nodejs apps you can force the user agent "browser" to rather download content instead of displaying or attempting to render given content within the browser.

In this example assuming you're using express.static to serve content pass an Object as second argument to express.static function

An Object with configuration parameters; const config = {
setHeaders: res => res.set('Content-disposition',  'attachment')

In your code replace "path/to/media/" with the path pointing to the static content.

app.use('/media/', express.static('path/to/media/', config))
To test try accessing content at /media/*

unused css rules tree shaking elimination nodejs

Unused css rules elimination process is a lot more like tree shaking done by JavaScript bundlers for instance if you're familiar with how webpack eliminates dead code as a result tree shaking generates a much smaller bundle with only essential codePurifycss is a nodejs npm open source package that eliminates unused css
You can use purifycss to remove excess css from libraries such as bootstrap, materal design lite to name a few,InstallationYou can get "purify-css" npm package from the package manager by typing in your terminal;npm i purify-cssTo install globally add -g flag;npm i -g purify-cssBasic example usage;
You can use css purify global cli package as;Syntax;purifycss {css} {html}Important flags;-m minify output
-o specify output file nameFor example;purifycss *.css *.html -o css.min.cssOutput containg minified css rules will get saved to css.min.css

How to make a static http server in nodejs using express

Express is a fast unopinionated, minimalist web framework for nodejs Authored by  TJ Holowaychuk
Express is a framework for building robust http servers, single page applications and API's among others.

How to create an http server in express

Open your text editor and terminal,
run the commands below to create a project directory, application file
and a landing page

mkdir project
cd project
mkdir static
echo "Hello World" > static index.html
touch app.js

You must have a directory structure as follows; |Project/

Project directory is the root directory of the application Static directory is where static files, webpages will be stored.

index.html is an index page with text "Hello world".

app.js is a blank JavaScript file, open your editor and paste the following few lines of code into app.js

import express from 'express' const app = express() const port = 3000 app.use('/', express.static('static…

Npm how to install webpack nodejs

A guide on how to install webpack bundler and webpack-cli using npm package manager first thing install webpack it's recommended not to install webpack globally to avoid conflicts with webpack versions npm i webpack -D
Note it's installed as a developer dependency. You'll also need to install webpack-cli
npm i webpack-cli -D
Again as a developer dependency.

hello world application in nodejs

Nodejs is a JavaScript command line runtime using chromes v8 engine known for its wicked fast speed and execution time. its well known that  the v8 engine compiles JavaScript to archive its impressive performance. Nodejs is a command line application ie the output will be forwarded to the console in our case the terminal of your shell. In your terminal or text editor type
console.log("Hello World") save the file simply as helloworld.js
In your terminal type node helloworld.js
Output // Hello World
// Hooray You dit it.

Npm nodejs yarn install

Assuming you have installed nodejs on your system next thing is installing developer build tools and yarn is a must yarn is a nodejs package manager similar but different to npm which is installed by default immediately you install nodejsOpen a terminal session onto it type
sudo npm install yarn -gYarn is going to have a symbolic link in the bin so don't omit sudo or you'll probably get an ugly error if you are not a super user ie root.lastly -g argument specifies the package should be installed globally

How to install nodejs on linux

Nodejs is an open source JavaScript runtime without a dom model nodejs can be used to create web servers api's  and command line applications among others its important to note nodejs is increasing popularity due to its simplicity , speed and developer friendliness i.e frontend developers once limited to using JavaScript in the browser can now create server side backend your terminal execute the command below to add node js repository.
curl -sL | sudo -E bash -update apt packages list
sudo apt apt update && upgradeinstall latest nodejs
sudo apt install nodejs