我试图将nodejs中的以下片段转换为typescript:如何在Nodejs中创建Http请求
这是我的TypeScript代码:
import * as http from 'http';
export class HttpRequest{
url: string;
private path: string;
private host: string;
private args: Array<Array<string>>;
constructor(url: string, args?: string){
this.url = url;
this.processUrl(this.url);
if(!!args){
this.processArgs(args);
}
this.args = [];
}
private processUrl(url: string): void {
let tld: number = url.lastIndexOf('.')
let sep: number = url.indexOf('/', tld);
this.host = url.slice(0, sep);
this.path = url.slice(sep+1);
}
private processArgs(args: string): void {
let sep: number = args.indexOf('&');
if(sep < 0){
return ;
}
let argpair: …Run Code Online (Sandbox Code Playgroud) 我正在尝试一个简单的应用程序.我有一个Express后端,在访问时返回一个JSON字符串localhost:4201/ticker.当我运行服务器并从我的Angular服务请求此链接时http,我收到以下错误:
XMLHttpRequest无法加载localhost:4201/ticker.交叉源请求仅支持协议方案:http,数据,chrome,chrome-extension,https.
我阅读了以下文章:了解和使用CORS,如上所述,将cors模块与我的快速服务器一起使用.但是,我仍然得到上面给出的错误.部分代码如下:
服务器代码:
private constructor(baseUrl: string, port: number) {
this._baseUrl = baseUrl;
this._port = port;
this._express = Express();
this._express.use(Cors());
this._handlers = {};
this._hInstance = new Handlers();
this.initHandlers();
this.initExpress();
}
private initHandlers(): void {
// define all the routes here and map to handlers
this._handlers['ticker'] = this._hInstance.ticker;
}
private initExpress(): void {
Object.keys(this._handlers)
.forEach((key) => {
this._express.route(this._url(key))
.get(this._handlers[key]);
});
}
private _url(k: string): string {
return '/' + k;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的 Angular 应用程序与 express 上的简单 REST 服务器连接起来。服务器只发送json数据响应请求。为了增加CORS支持,我使用了corsnpm 中的模块。在 Angular 应用程序上,我添加了HttpHeaders以下问题的说明:Angular CORS request blocks。
这是我在 express 中设置 cors 选项的代码:`
// async CORS setup delegation
function corsOptsDelegator(req, cb) {
let opts = {},
origin = req.header('Origin');
if(imports.allowedOrigins.indexOf(origin) === 1) opts.origin = true;
else opts.origin = false;
opts.optionsSuccessStatus = 200;
opts.methods = ['POST', 'GET']; // allowed methods
opts.credentials = true; // for passing/setting cookie
opts.allowedHeaders = ['Content-Type', 'Accept', 'Access-Control-Allow-Origin']; // restrict headers
opts.exposedHeaders …Run Code Online (Sandbox Code Playgroud) 我用tweepy挖掘用户时间线数据,并且在理解以下方面遇到了一些困难:
感谢您的帮助.
这可能是一个常见问题,如果有更好的答案,请指点我。话虽如此,问题来了:
在顶层,我正在开发的 angular 应用程序公开了一个登录路径和 4 个独立仪表板的路径,具体取决于谁登录。这部分按预期工作。
对于每个仪表板,我都有一个大致相同的侧面导航(某些选项仅针对某些类型的用户显示)。在仪表板中,我有一个嵌套的路由器插座。每当我尝试在嵌套插座内加载模块时,Angular 都无法匹配路径。这是我到目前为止所拥有的:
app-routing.module.ts
const routes: Routes = [
{ path: '', pathMatch: 'full', redirectTo: 'login' },
{ path: 'login', loadChildren: () => import('./modules/auth/auth.module').then(m => m.AuthModule) },
//{ path: 'dashboard', loadChildren: () => import('./modules/dashboard/dashboard.module').then(m => m.DashboardModule) }
{ path: 'admin', loadChildren: () => import('./modules/admin/admin.module').then(m => m.AdminModule) },
];
Run Code Online (Sandbox Code Playgroud)
admin-routing.module.ts
const routes: Routes = [
{ path: '', pathMatch: 'full', component: AdminComponent, children: [
{ path: 'employee', loadChildren: () => import('./../employee/employee.module').then(m => m.EmployeeModule) },
{ path: …Run Code Online (Sandbox Code Playgroud) 我在 Rust 中有以下代码。我知道我不应该返回对局部变量的引用,在这种情况下我不是。的字符串分割为被传递&str的参考和,求出分割边界之后,我返回&s[0..idx]其中idx是边界的末尾。我相信这不会导致“悬空”参考相关错误。然而,事实证明我错了!
fn demo4() {
let mut s = String::from("Elijah Wood");
let firstname = str_split(&s, &String::from(" "));
println!("First name of actor: {}", firstname);
}
// can handle both &str and &String
fn str_split(s: &str, pat: &str) -> &str {
let bytes = s.as_bytes();
let b_pat = pat.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b_pat {
return &s[0..i];
}
}
&s[..]
}
fn main() {
demo4();
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
fn demo4() { …Run Code Online (Sandbox Code Playgroud) 这可能是一个简单的错误,但我似乎无法certbot用来验证我的域。我正在使用连接到快速应用程序的 nginx。我已经注释掉了默认 nginx 文件中的配置,它只包含我的站点的配置/etc/nginx/conf.d/mysite.info。在我的配置中,第一个位置条目指向根/.well-known/acme-challenge目录。这是我的 nginx conf 文件中的设置:
server {
listen 80;
server_name <MYDOMAIN>.info www.<MYDOMAIN>.info;
location '/.well-known/acme-challenge' {
root /srv/www/<MY_ROOT_DIRECTORY>;
}
location / {
proxy_pass http://localhost:4200;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /secure {
auth_pam "Secure zone";
auth_pam_service_name "nginx";
}
}
Run Code Online (Sandbox Code Playgroud)
为了验证,我使用了以下 certbot 命令:
certbot certonly --agree-tos --email <My_EMAIL>@gmail.com --webroot -w /srv/www/<ROOT_FOLDER>/ -d <DOMAIN>.info
Run Code Online (Sandbox Code Playgroud)
certbot的错误如下:
Performing the following challenges:
http-01 challenge for <MYDOMAIN>.info
Using the webroot …Run Code Online (Sandbox Code Playgroud) 我正在尝试包含一个ejs文件,该文件包含用于设置视图的功能。这些功能被定义为可以使用的助手。我试过使用:
<% include helpers.ejs %>
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用此文件中的函数时:
<% module_start('$body', [{name:'display',value:'block'}], 'body'); %>
Run Code Online (Sandbox Code Playgroud)
我得到错误:
Reference Error: module_start is not defined
Run Code Online (Sandbox Code Playgroud)
当我将代码从“ helpers.ejs”复制到我的原始视图文件“ test.ejs”时,它可以正常工作。我经历了几个答案,但仍然感到困惑,在这种情况下我做错了什么。
感谢您的帮助。
我正在努力实现以下目标:
mat-toolbar组件,app.component.html用于显示网站的主要顶部导航栏。工具栏下面是<router-outlet></router-outlet>。localhost:4200/我的根目录时,我希望顶部导航栏可见,并且与导航栏中的链接关联的所有组件都呈现在顶部导航栏下方。这里是 app.module.ts
// initialization and module imports
const RootRoutes: Routes = [
{path: "", redirectTo: "root", pathMatch: "full"},
{path: "root", component: AppComponent, },
//{path: "home", component: HomeComponent },
{path: "fiction", component: FictionDisplayComponent },
{path: "nonfiction", component: NonFictionDisplayComponent},
{path: 'poetry', component: PoetryDisplayComponent},
{path: 'journal', component: JournalDisplayComponent},
{path: 'letters', component: PersonalLetterDisplayComponent},
{path: 'jokes', component: JokesDisplayComponent}
];
// NgModule declaration
Run Code Online (Sandbox Code Playgroud)
这里是如何app.component.html的设置:
<div class="pure-g rr-main-wrapper">
<div class="pure-u-5-5 home-top-navbar-wrapper">
<rr-home-top-navbar></rr-home-top-navbar>
</div> …Run Code Online (Sandbox Code Playgroud) 我正在后端系统上开始开发。它将支持跨平台移动应用程序。后端有许多功能,这促使我将后端划分为 8 个服务(部署为 Cloud Functions)并作为 REST API 公开给客户端。每个部署的函数都将使用 firestore 并将严格查询与该特定服务相关的集合。严格禁止通过 HTTP 进行服务间通信,并且所有此类通信都仅限于云消息传递。
现在,我最近被告知 Firebase 将成为“瓶颈”并且无法处理扩展。我非常有信心扩展不会成为问题,因为扩展服务仅意味着增加已部署功能实例的数量。数据库(Firestore)也是如此。从一般的角度来看,我可以说 Firebase 具有可扩展性吗?我知道有很多挑战,这是一个广泛的问题。但是,当我在 VPS 上进行自己的设置时,这些挑战甚至会存在。所以,澄清我的问题:从后端的角度来看,它有许多作为 REST API 开发的服务。Firebase 是可扩展的选项吗?任何建议、参考或指南将不胜感激。
scalability firebase google-cloud-functions google-cloud-firestore
angular ×4
cors ×2
express ×2
certbot ×1
ejs ×1
firebase ×1
javascript ×1
lets-encrypt ×1
lifetime ×1
nginx ×1
node.js ×1
python ×1
rust ×1
scalability ×1
tweepy ×1
typescript ×1