Skip to content

FlutterKerala/DServers

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DServers

Dart Server

Intro

This package will help you to start Dart servers easily.

how to run dart code

Official Dart Dev

Example

GitHub Repo Dservers Example

How to work

Initialise a main with Class

void main() => MyApp();

now extend the DServer with you class now you can acess DSerevers

class MyApp extends DServers {
  MyApp() {
   
   }
}

Now to start your server use start Server

startServer(
 ipAdress: InternetAddress.loopbackIPv4,
 port: 8000,
 onServerStart: (){},
 onurlNotFound:(){}
 parameters: []
 )

The StartServer can take a few parameters. They are ipaddress take the ipaddress of the server need to host, port is the needed port that need to show your server.

onServerStart

This will take a function which has parameters to set up the server. This server will call when the server starts. For example:

onServerStart: (server) {
          print(
              'ServerStart ${server.port} and ip adress ${InternetAddress.loopbackIPv4}');
        }

The onStart will get a function which have server as the parameter with server we can get things about our servers like server.port for getting in which server do our machine runs.

onurlNotFound

This is function that dev can give if they need a custom 404 error page. This function will trigger if there is a 404 error on the server request. For example:

 onurlNotFound: (request) {
          request.response
            ..write('Sorry This is 404 Error')
            ..close();
        }

onurlNotFound has a function as a parameter for request. We can get out the response when this function is called to users. Also we can set server status etc.To set server status you can add:

statusCode =HttpStatus.notFound

For example:

 onurlNotFound: (request) {
          request.response
          ..statusCode =HttpStatus.notFound
            ..write('Sorry This is 404 Error')
            ..close();
        }

Parameters

It will take a list of parameters which takes in a parameters object.

parameters:[ 
  Parameters(),
  Parameters(),
]

Parameters object

The Parameters object takes parameters which is a string type representing the url

Parameters Parameters({String parameters, Function onRequest})

For example:

Parameters(
              parameters: '/',
              onRequest: (request) {
                request.response
                  ..write('Welcome to DServer')
                  ..close();
              })

parameters url

The parameters will get a string which represent a string

String parameters

onRequest

This will take a function which has a parameters request. This function will trigger when user acess those urls. For Example:

onRequest: (request) {
                request.response
                  ..write('Welcome to DServer')
                  ..close();
              }

the Request parameter can be used to do response whenever the function is called. Users can also set server status by

statusCode =HttpStatus.<status>

In this you get which method is called on server by

request.method

Method can be any of GET, POST, UPDATE etc....

You can view HTML with this package or Dart language as a response. But I recommend you to use this as an api server to connect your Flutter web or app. Flutter provides much more in their Flutter Web and you can still write website in one code base instead of writing HTML.

Future

This package was developed to show that Dart is not only a client side language but you can also use it serverside.

About

This is the DServer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 100.0%