1347 views|1 replies

247

Posts

4

Resources
The OP
 

[Domestic RISC-V Linux board Fang·Starlight VisionFive trial report] Nodejs development on Starlight board 2: web server [Copy link]

 

In the previous article, I shared the basic nodejs development on the Starlight board and realized the use of nodejs to control the LED flashing.

This time, I will share how to build a basic web service on the Starlight board and control the LED through the web service.

First of all, nodejs comes with an http module, through which you can build an http server to provide external web services; you can also build an http client to obtain data provided by other web servers.

By viewing the official nodejs http documentation: http Hypertext Transfer Protocol | Node.js API documentation (nodejs.cn)

Finished the development data of nodejs http and wrote the following program:

var http = require('http');
var fs = require('fs');
var url = require('url');
 
 
// 创建服务器
http.createServer( function (request, response) {  
   // 解析请求,包括文件名
   var pathname = url.parse(request.url).pathname;
   
   // 输出请求的文件名
   console.log("Request for " + pathname + " received.");
   
   response.writeHead(404, {'Content-Type': 'text/html'});
   response.write(pathname);
   response.end();

}).listen(8080);
 
// 控制台会输出以下信息
console.log('Server running at http://*.*.*.*:8080/');

Save the above code as web_server.js, and then execute it using nodejs:

After execution, it will display “Server running at http://*.*.*.*:8080/”, indicating that the server is accessible.

Then visit the following website: (Please set the IP address based on the actual IP address of your development board)

As you can see, the web server is able to receive our request.

Then, we can combine the previous sharing of nodejs controlling LED to control LED through the web page:

var http = require('http');
var fs = require('fs');
var url = require('url');

const Gpio = require('onoff').Gpio;
const led = new Gpio(448, 'out');

// 创建服务器
http.createServer( function (request, response) {  
   // 解析请求,包括文件名
   var pathname = url.parse(request.url).pathname;
   
   // 输出请求的文件名
   console.log("Request for " + pathname + " received.");
   
   if(pathname=="/on") {
    led.write(1);   
   } else if(pathname=="/off") {
    led.write(0);
   }
   response.writeHead(404, {'Content-Type': 'text/html'});
   response.write(pathname);
   response.end();

}).listen(8080);
 
// 控制台会输出以下信息
console.log('Server running at http://*.*.*.*:8080/');

Save the above code as web_led.js, and then execute:

Then visit the URL corresponding to /on and /off, and the LED can be controlled through the web page.

By referring to similar methods, you can also learn to control other types of peripherals and read display data.

This post is from Domestic Chip Exchange

Latest reply

Build a basic web service on the Starlight board to control the LED through the web service, and learn   Details Published on 2022-7-3 21:54
 
 

6555

Posts

0

Resources
2
 

Build a basic web service on the Starlight board to control the LED through the web service, and learn

This post is from Domestic Chip Exchange
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list