1. Write at the front

Local development, data retrieval will encounter cross-domain problems, see the console red string error:At this time you work overtime, must have been the mouth spit fragrance, open a web page search, shop surface but come is “solve cross domain 8 methods”, “what is cross domain”… A thousand horses galloping inside… I don’t listen, I don’t listen, I just want to get the data…


  • To get back to the basics, we’ll talk about how to start nginx in your project (Windows) using the NPM scripts script command. That is, configure your own commands in the package.json directory of the project root directory. NPM run nginx:start can start nginx, so it is not very convenient, pretend to be very convenient, manual head.
  • Sudo brew install nginx can be used to install nginx on a MAC. Nginx can be used to install nginx on a MAC

2. Shell script learning

Consider starting Nginx with a shell script and then using Node to execute the shell so that you can start Nginx with a single command. Shell tutorial, to learn some simple Shell.

1. The variable

Curly braces are recommended for all variables as a good programming practice.

#! /bin/bash



name="jack"

echo hello ${name}

# hello jack

Copy the code

2. The string

You are advised to use double quotation marks, which can contain variables and escape characters

#! /bin/bash



str1='this is a string'

name="jack"

age="18"

str2=str="hello \"${name}\ ",${age} years old"

echo -e ${str}

# hello "jack", 18 years old

Copy the code

3. Shell flow control -if else

if condition

then

    command1 

    command2

.

    commandN 

fi

Copy the code

If determines multiple conditional writing

 if (( a > b )) && (( a < c )) 

Copy the code

Or I could write it this way

 if [[ $a > $b[[]] &&$a < $c ]] 

Copy the code

4. Shell process control-case

case 值 in

Model 1)

    command1

    command2

.

    commandN

    ;;

Model 2)

    command1

    command2

.

    commandN

    ;;

esac

Copy the code

5. The Shell passes parameters

When executing a Shell script, we can pass parameters to the script in the format of $n. N is a number. 1 is the first parameter of the script, 2 is the second parameter of the script, and so on……

#! /bin/bash



echo "Execute file name:$0";

echo "The first parameter is:The $1";

echo "The second parameter is:$2";

Copy the code

The following output is displayed:

$ chmod +x test.sh 

$ ./test.sh 1 2 3

# Shell pass argument instance!

# Execute the file name:./test.sh

The first parameter is: 1

The second parameter is: 2

Copy the code

3. Write our own shell scripts

1. Project Catalog

├ ─ ─ nginx

├ ─ ─ node_modules

├ ─ ─ package. Json

├ ─ ─ the public

├ ─ ─ the README, md

├ ─ ─ the SRC

├ ─ ─ nginx. Sh

├ ─ ─ the download. Sh

├ ─ ─ nginx. Conf

└ ─ ─ yarn. The lock



Copy the code

2. The train of thought

Divided intodownload.shDownload nginx to use andnginx.shStart nginx use

3. nginx.sh

#! /bin/bash

CRTDIR=$(cd $(dirname $0); pwd# current directory

NGINX_DIR="nginx"

cd "${CRTDIR}/${NGINX_DIR}" Cut to the current nginx root directory



NGINX="./nginx.exe"

NGINX_CONF="./conf/nginx.conf" # nginx configuration file for the current directory

Start="start ${NGINX}" # start nginx

Stop="${NGINX} -s stop" # close

Reload="${NGINX} -s reload" # overloading



case "The $1" in 

  "start")

    echo "nginx start"

    # https://blog.csdn.net/yf210yf/article/details/9207335

    cp ${CRTDIR}/nginx.conf -f ${CRTDIR}/${NGINX_DIR}/conf # copy root directory nginx.conf to /nginx/conf

    ${Start} -c ${NGINX_CONF}

    ;;

  "stop")

    echo "nginx stop"

    ${Stop}

    ;;

  "reload")

    echo "nginx reload"

    ${Reload}

    ;;

  *)

    echo commond not exist!

    exit 1

esac

Copy the code

4. download.sh

#! /bin/bash

CRTDIR=$(cd $(dirname $0); pwd# current directory

NGINX_DIR="nginx"

NGINX_File="nginx/nginx.exe"

NGINX_VERSION="1.16.0"

NGINX_RENAME="nginx.zip"

downloadPath="http://nginx.org/download/nginx-${NGINX_VERSION}.zip"



# https://www.jb51.net/article/102277.htm

if [[ -d "${NGINX_DIR}" ]] && [[ -f "${NGINX_File}" ]]

then

  echo "nginx found, nginx start..."

  # cp ./nginx.conf -f ${CRTDIR}/${NGINX_DIR}/conf

  # call nginx. Sh

  source ./nginx.sh start 

else

  echo "nginx not found, downloading nginx..."

  Delete old files

  rm -rf "${CRTDIR}/${NGINX_DIR}"

  rm -rf "${CRTDIR}/${NGINX_RENAME}"

  # wget "$downloadPath" -O nginx.zip && unzip nginx.zip -d .

  # download nginx

  Unzip to the appropriate directory

  Rename the folder

  Delete the compressed file

  # copy root directory nginx.conf to /nginx/conf

  # start nginx

  curl -o "${NGINX_RENAME}" "${downloadPath}"

  unzip "${NGINX_RENAME}" -d .

  mv "${CRTDIR}/nginx-${NGINX_VERSION}" "${CRTDIR}/${NGINX_DIR}"

  rm -f "${NGINX_RENAME}"

  cp ./nginx.conf -f ${CRTDIR}/${NGINX_DIR}/conf

  source ./nginx.sh start

fi

Copy the code

5. Configure the scripts command in package.json

"scripts": {

  "download""bash download.sh".

  "nginx:start""sh ./nginx.sh start".

  "nginx:stop""sh ./nginx.sh stop".

  "nginx:reload""sh ./nginx.sh reload"

}

Copy the code

Start, close, and reload nginx

# download nginx

npm run download

# start nginx

npm run nginx:start

Close # nginx

npm run nginx:stop

# reload nginx

npm run nginx:reload

Copy the code

Start the nginx

$ yarn run nginx:start

Yarn run v1.13.0

$ sh ./nginx.sh start

nginx start

Done in0.28 s.

Copy the code

Close the nginx

$ yarn run nginx:stop

Yarn run v1.13.0

$ sh ./nginx.sh stop

nginx stop

Done in0.23 s.



Copy the code

4. Pay attention to the point

In Windows, use Git bash to run the name, or download the cmder and run the command.

5. Reference materials

  1. Shell Tutorial
  2. Nginx download
  3. Install Nginx on MAC
  4. Shell script —-cp (copy) Copies files or directories
  5. Method of checking for the existence of files using Bash Shell