marp | theme | title | description | url |
---|---|---|---|---|
true |
uncover |
NestJS vs AspNET |
Comparison of web server frameworks |
with me, Kyle π
ASP.βNET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications on top of TypeScript & JavaScript (ES6, ES7, ES8) π
Fully Open Source - Free as in Freedom π
Well written docs π
Evolutions of Prior Art π
- aspnet - built on top of dotnet core
- nestjs - built on top of node / express
A common paradigm for your data π
What you'll need to get started π»
dotnet tool install --global dotnet-aspnet-codegenerator
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
npm install @nestjs/cli -g
Hello World π
dotnet new mvc -o aspnet
git clone https://github.com/nestjs/typescript-starter.git nestjs --depth 1
cd nestjs
npm install
Getting the project up and running π©βπ»
dotnet run # run once
dotnet watch run # run with watch
open http://localhost:5000
npm run start # run once
npm run start:dev # run with watch
open http://localhost:3000
// Program.cs
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
// main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
nest generate controller weather
dotnet aspnet-codegenerator controller -name weather
// Controllers/WeatherController.cs
[Route("weather")]
public class WeatherController : Controller
{
[HttpGet]
public IActionResult GetWeather()
{
return Content("Frightful");
}
}
// weather.controller.ts
@Controller('weather')
export class WeatherController {
@Get()
getWeather(): string {
return 'Frightful';
}
}
[HttpGet]
[Route("light")]
public IActionResult GetDaylight([FromQuery] int hour)
{
var result = hour < 6 || hour > 18 ? "Dark" : "Light";
return Content(result);
}
@Get('light')
getDaylight(@Query('hour') hour: number): string {
console.log(hour);
return hour < 6 || hour > 18 ? 'Dark' : 'Light';
}
public class Home
{
public Home(string msg)
{
this.Message = msg;
}
public string Message { get; set; }
}
@Injectable()
export class Home {
message: string;
constructor(msg: string) {
this.message = msg;
}
}
Neither solution comes with views in the bare bones setup.
Both are well supported
// startup.cs
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddMvcCore().AddRazorViewEngine();
// ...
}
npm install --save hbs
// main.ts
app.useStaticAssets(join(__dirname, '..', 'public'));
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('hbs');
[Route("home")]
public class HomeController : Controller
{
public IActionResult Index()
{
var model = new Home("Hi There!");
return View(model);
}
}
@Controller('home')
export class HomeController {
@Get()
@Render('index')
root() {
const model = new Home('Hello');
return model;
}
}
Razor Syntax
// views/home/index.cshtml
@model.Message