20201124 20201124 mkdir myapp cd myapp 20201124 Express

  • Slides: 26
Download presentation

2020/11/24

2020/11/24

2020/11/24

2020/11/24

>mkdir myapp & cd myapp 2020/11/24

>mkdir myapp & cd myapp 2020/11/24

安裝Express >npm init

安裝Express >npm init

安裝Express https: //expressjs. com/en/starter/generator. html

安裝Express https: //expressjs. com/en/starter/generator. html

2020/11/24

2020/11/24

>npm install express --save 2020/11/24

>npm install express --save 2020/11/24

>npm install express-generator -g 2020/11/24

>npm install express-generator -g 2020/11/24

const express = require('express'); const app = express(); //建立一個Express伺服器 app. get('/', function (req, res)

const express = require('express'); const app = express(); //建立一個Express伺服器 app. get('/', function (req, res) { res. send('Express is excellent!') }); app. listen(3000, function () { console. log('Example app is running on port 3000!'); } ); 2020/11/24

2020/11/24

2020/11/24

https: //codeburst. io/build-a-weather-website-in-30 -minutes-with-node-jsexpress-openweather-a 317 f 904897 b How to use EJS in Express

https: //codeburst. io/build-a-weather-website-in-30 -minutes-with-node-jsexpress-openweather-a 317 f 904897 b How to use EJS in Express http: //robdodson. me/how-to-use-ejs-inexpress/ Express 教學 2: 創建一個骨架網站 https: //developer. mozilla. org/zh-TW/docs/Learn/Serverside/Express_Nodejs/skeleton_website

/* DHCP-based IP printer This sketch uses the DHCP extensions to the Ethernet library

/* DHCP-based IP printer This sketch uses the DHCP extensions to the Ethernet library to get an IP address via DHCP and print the address obtained. using an Arduino Wiznet Ethernet shield. Circuit: Ethernet shield attached to pins 10, 11, 12, 13 created 12 April 2011 modified 9 Apr 2012 by Tom Igoe modified 02 Sept 2015 by Arturo Guadalupi */ #include <SPI. h> #include <Ethernet. h> // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0 x 9 E, 0 x 15, 0 x 1 E, 0 x 41, 0 x 62, 0 x 5 B }; void setup() { // You can use Ethernet. init(pin) to configure the CS pin //Ethernet. init(10); // Most Arduino shields //Ethernet. init(5); // MKR ETH shield //Ethernet. init(0); // Teensy 2. 0 //Ethernet. init(20); // Teensy++ 2. 0 //Ethernet. init(15); // ESP 8266 with Adafruit Featherwing Ethernet //Ethernet. init(33); // ESP 32 with Adafruit Featherwing Ethernet // Open serial communications and wait for port to open: Serial. begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // start the Ethernet connection: Serial. println("Initialize Ethernet with DHCP: "); if (Ethernet. begin(mac) == 0) { Serial. println("Failed to configure Ethernet using DHCP"); if (Ethernet. hardware. Status() == Ethernet. No. Hardware) { Serial. println("Ethernet shield was not found. Sorry, can't run without hardware. : ("); } else if (Ethernet. link. Status() == Link. OFF) { Serial. println("Ethernet cable is not connected. "); } // no point in carrying on, so do nothing forevermore: while (true) { delay(1); } } // print your local IP address: Serial. print("My IP address: "); Serial. println(Ethernet. local. IP()); } void loop() { switch (Ethernet. maintain()) { case 1: //renewed fail Serial. println("Error: renewed fail"); break; case 2: //renewed success Serial. println("Renewed success"); //print your local IP address: Serial. print("My IP address: "); Serial. println(Ethernet. local. IP()); break; case 3: //rebind fail Serial. println("Error: rebind fail"); break; case 4: //rebind success Serial. println("Rebind success"); //print your local IP address: Serial. print("My IP address: "); Serial. println(Ethernet. local. IP()); break; default: //nothing happened break; } }

/* DHCP Chat Server A simple server that distributes any incoming messages to all

/* DHCP Chat Server A simple server that distributes any incoming messages to all connected clients. To use, telnet to your device's IP address and type. You can see the client's input in the serial monitor as well. Using an Arduino Wiznet Ethernet shield. THis version attempts to get an IP address using DHCP Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 created 21 May 2011 modified 9 Apr 2012 by Tom Igoe modified 02 Sept 2015 by Arturo Guadalupi Based on Chat. Server example by David A. Mellis */ #include <SPI. h> #include <Ethernet. h> // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: byte mac[] = { 0 x 44, 0 x 8 A, 0 x 5 B, 0 x. A 0, 0 x. C 8, 0 x 3 B }; IPAddress ip(192, 168, 0, 106); IPAddress my. Dns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 0, 0); // telnet defaults to port 23 Ethernet. Server server(23); bool got. AMessage = false; // whether or not you got a message from the client yet void setup() { // You can use Ethernet. init(pin) to configure the CS pin //Ethernet. init(10); // Most Arduino shields //Ethernet. init(5); // MKR ETH shield //Ethernet. init(0); // Teensy 2. 0 //Ethernet. init(20); // Teensy++ 2. 0 //Ethernet. init(15); // ESP 8266 with Adafruit Featherwing Ethernet //Ethernet. init(33); // ESP 32 with Adafruit Featherwing Ethernet // Open serial communications and wait for port to open: Serial. begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // start the Ethernet connection: Serial. println("Trying to get an IP address using DHCP"); if (Ethernet. begin(mac) == 0) { Serial. println("Failed to configure Ethernet using DHCP"); // Check for Ethernet hardware present if (Ethernet. hardware. Status() == Ethernet. No. Hardware) { Serial. println("Ethernet shield was not found. Sorry, can't run without hardware. : ("); while (true) { delay(1); // do nothing, no point running without Ethernet hardware } } if (Ethernet. link. Status() == Link. OFF) { Serial. println("Ethernet cable is not connected. "); } // initialize the Ethernet device not using DHCP: Ethernet. begin(mac, ip, my. Dns, gateway, subnet); } // print your local IP address: Serial. print("My IP address: "); Serial. println(Ethernet. local. IP()); // start listening for clients server. begin(); } void loop() { // wait for a new client: Ethernet. Client client = server. available(); // when the client sends the first byte, say hello: if (client) { if (!got. AMessage) { Serial. println("We have a new client"); client. println("Hello, client!"); got. AMessage = true; } // read the bytes incoming from the client: char this. Char = client. read(); // echo the bytes back to the client: server. write(this. Char); // echo the bytes to the server as well: Serial. print(this. Char); Ethernet. maintain(); } }

#include <Wire. h> #include <Adafruit_Sensor. h> #include <Adafruit_ADXL 345_U. h> /* Assign a unique

#include <Wire. h> #include <Adafruit_Sensor. h> #include <Adafruit_ADXL 345_U. h> /* Assign a unique ID to this sensor at the same time */ Adafruit_ADXL 345_Unified accel = Adafruit_ADXL 345_Unified(12345); void display. Sensor. Details(void) { sensor_t sensor; accel. get. Sensor(&sensor); Serial. println("------------------"); Serial. print ("Sensor: "); Serial. println(sensor. name); Serial. print ("Driver Ver: "); Serial. println(sensor. version); Serial. print ("Unique ID: "); Serial. println(sensor_id); Serial. print ("Max Value: "); Serial. print(sensor. max_value); Serial. println(" m/s^2"); Serial. print ("Min Value: "); Serial. print(sensor. min_value); Serial. println(" m/s^2"); Serial. print ("Resolution: "); Serial. print(sensor. resolution); Serial. println(" m/s^2"); Serial. println("------------------"); Serial. println(""); delay(500); } void display. Data. Rate(void) { Serial. print ("Data Rate: "); switch(accel. get. Data. Rate()) { case ADXL 345_DATARATE_3200_HZ: Serial. print ("3200 "); break; case ADXL 345_DATARATE_1600_HZ: Serial. print ("1600 "); break; case ADXL 345_DATARATE_800_HZ: Serial. print ("800 "); break; case ADXL 345_DATARATE_400_HZ: Serial. print ("400 "); break; case ADXL 345_DATARATE_200_HZ: Serial. print ("200 "); break; case ADXL 345_DATARATE_100_HZ: Serial. print ("100 "); break; case ADXL 345_DATARATE_50_HZ: Serial. print ("50 "); break; case ADXL 345_DATARATE_25_HZ: Serial. print ("25 "); break; case ADXL 345_DATARATE_12_5_HZ: Serial. print ("12. 5 "); break; case ADXL 345_DATARATE_6_25 HZ: Serial. print ("6. 25 "); break; case ADXL 345_DATARATE_3_13_HZ: Serial. print ("3. 13 "); break; case ADXL 345_DATARATE_1_56_HZ: Serial. print ("1. 56 "); break; case ADXL 345_DATARATE_0_78_HZ: Serial. print ("0. 78 "); break; case ADXL 345_DATARATE_0_39_HZ: Serial. print ("0. 39 "); break; case ADXL 345_DATARATE_0_20_HZ: Serial. print ("0. 20 "); break; case ADXL 345_DATARATE_0_10_HZ: Serial. print ("0. 10 "); break; default: Serial. print ("? ? "); break; } Serial. println(" Hz"); } void display. Range(void) { Serial. print ("Range: +/- "); switch(accel. get. Range()) { case ADXL 345_RANGE_16_G: Serial. print ("16 "); break; case ADXL 345_RANGE_8_G: Serial. print ("8 "); break; case ADXL 345_RANGE_4_G: Serial. print ("4 "); break; case ADXL 345_RANGE_2_G: Serial. print ("2 "); break; default: Serial. print ("? ? "); break; } Serial. println(" g"); } void setup(void) { #ifndef ESP 8266 while (!Serial); // for Leonardo/Micro/Zero #endif Serial. begin(9600); Serial. println("Accelerometer Test"); Serial. println(""); /* Initialise the sensor */ if(!accel. begin()) { /* There was a problem detecting the ADXL 345. . . check your connections */ Serial. println("Ooops, no ADXL 345 detected. . . Check your wiring!"); while(1); } /* Set the range to whatever is appropriate for your project */ accel. set. Range(ADXL 345_RANGE_16_G); // accel. set. Range(ADXL 345_RANGE_8_G); // accel. set. Range(ADXL 345_RANGE_4_G); // accel. set. Range(ADXL 345_RANGE_2_G); /* Display some basic information on this sensor */ display. Sensor. Details(); /* Display additional settings (outside the scope of sensor_t) */ display. Data. Rate(); display. Range(); Serial. println(""); } void loop(void) { /* Get a new sensor event */ sensors_event_t event; accel. get. Event(&event); /* Display the results (acceleration is measured in m/s^2) */ Serial. print("X: "); Serial. print(event. acceleration. x); Serial. print(" "); Serial. print("Y: "); Serial. print(event. acceleration. y); Serial. print(" "); Serial. print("Z: "); Serial. print(event. acceleration. z); Serial. print(" "); Serial. println("m/s^2 "); delay(500); }